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 "event_handler.h"
21 #include "graphic_common.h"
22 #include "vsync_log.h"
23
24 namespace OHOS {
25 namespace Rosen {
26 namespace {
27 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0, "VsyncReceiver" };
28 constexpr int32_t INVALID_FD = -1;
29 }
OnReadable(int32_t fileDescriptor)30 void VSyncCallBackListener::OnReadable(int32_t fileDescriptor)
31 {
32 if (fileDescriptor < 0) {
33 return;
34 }
35 int64_t now;
36 ssize_t retVal = read(fileDescriptor, &now, sizeof(int64_t));
37 VSyncCallback cb = nullptr;
38 {
39 std::lock_guard<std::mutex> locker(mtx_);
40 cb = vsyncCallbacks_;
41 }
42 VLOGI("retVal:%{public}d, cb == nullptr:%{public}d", retVal, (cb == nullptr));
43 if (retVal > 0 && cb != nullptr) {
44 ScopedBytrace func("ReceiveVsync");
45 cb(now, userData_);
46 }
47 }
48
VSyncReceiver(const sptr<IVSyncConnection> & conn,const std::shared_ptr<OHOS::AppExecFwk::EventHandler> & looper,const std::string & name)49 VSyncReceiver::VSyncReceiver(const sptr<IVSyncConnection>& conn,
50 const std::shared_ptr<OHOS::AppExecFwk::EventHandler>& looper,
51 const std::string& name)
52 : connection_(conn), looper_(looper),
53 listener_(std::make_shared<VSyncCallBackListener>()),
54 init_(false),
55 fd_(INVALID_FD),
56 name_(name)
57 {
58 };
59
Init()60 VsyncError VSyncReceiver::Init()
61 {
62 std::lock_guard<std::mutex> locker(initMutex_);
63 if (init_) {
64 return VSYNC_ERROR_OK;
65 }
66 if (connection_ == nullptr) {
67 return VSYNC_ERROR_NULLPTR;
68 }
69
70 VsyncError ret = connection_->GetReceiveFd(fd_);
71 if (ret != VSYNC_ERROR_OK) {
72 return ret;
73 }
74
75 if (looper_ == nullptr) {
76 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(true);
77 looper_ = std::make_shared<AppExecFwk::EventHandler>(runner);
78 runner->Run();
79 }
80
81 looper_->AddFileDescriptorListener(fd_, OHOS::AppExecFwk::FILE_DESCRIPTOR_INPUT_EVENT, listener_);
82 init_ = true;
83 return VSYNC_ERROR_OK;
84 }
85
~VSyncReceiver()86 VSyncReceiver::~VSyncReceiver()
87 {
88 if (fd_ != INVALID_FD) {
89 looper_->RemoveFileDescriptorListener(fd_);
90 close(fd_);
91 fd_ = INVALID_FD;
92 }
93 }
94
RequestNextVSync(FrameCallback callback)95 VsyncError VSyncReceiver::RequestNextVSync(FrameCallback callback)
96 {
97 std::lock_guard<std::mutex> locker(initMutex_);
98 if (!init_) {
99 return VSYNC_ERROR_API_FAILED;
100 }
101 listener_->SetCallback(callback);
102 ScopedBytrace func("VSyncReceiver::RequestNextVSync_pid:" + std::to_string(getpid()) + "_name:" + name_);
103 return connection_->RequestNextVSync();
104 }
105
SetVSyncRate(FrameCallback callback,int32_t rate)106 VsyncError VSyncReceiver::SetVSyncRate(FrameCallback callback, int32_t rate)
107 {
108 std::lock_guard<std::mutex> locker(initMutex_);
109 if (!init_) {
110 return VSYNC_ERROR_API_FAILED;
111 }
112 listener_->SetCallback(callback);
113 return connection_->SetVSyncRate(rate);
114 }
115 } // namespace Rosen
116 } // namespace OHOS
117