1 /*
2 * Copyright (c) 2021-2023 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 "drm_vsync_worker.h"
17 #include <chrono>
18 #include "display_log.h"
19 #include "drm_device.h"
20
21 namespace OHOS {
22 namespace HDI {
23 namespace DISPLAY {
DrmVsyncWorker()24 DrmVsyncWorker::DrmVsyncWorker() {}
25
Init(int fd)26 int32_t DrmVsyncWorker::Init(int fd)
27 {
28 DISPLAY_CHK_RETURN((fd < 0), DISPLAY_FAILURE, DISPLAY_LOGE("the fd is invalid"));
29 mDrmFd = fd;
30 DISPLAY_LOGD("the drm fd is %{public}d", fd);
31 mThread = std::make_unique<std::thread>([this]() { WorkThread(); });
32 DISPLAY_CHK_RETURN((mThread == nullptr), DISPLAY_FAILURE, DISPLAY_LOGE("can not create thread"));
33 mRunning = true;
34 return DISPLAY_SUCCESS;
35 }
36
GetInstance()37 DrmVsyncWorker &DrmVsyncWorker::GetInstance()
38 {
39 static DrmVsyncWorker instance;
40 static std::once_flag once;
41 std::call_once(once, [&]() {
42 int ret = instance.Init(DrmDevice::GetDrmFd());
43 if (ret != DISPLAY_SUCCESS) {
44 DISPLAY_LOGE("Vsync Worker Init failed");
45 }
46 });
47 return instance;
48 }
49
~DrmVsyncWorker()50 DrmVsyncWorker::~DrmVsyncWorker()
51 {
52 DISPLAY_LOGD();
53 {
54 std::lock_guard<std::mutex> lg(mMutex);
55 mRunning = false;
56 }
57 DISPLAY_LOGD();
58 mCondition.notify_one();
59 if (mThread != nullptr) {
60 mThread->join();
61 }
62 DISPLAY_LOGD();
63 }
64
WaitSignalAndCheckRuning()65 bool DrmVsyncWorker::WaitSignalAndCheckRuning()
66 {
67 std::unique_lock<std::mutex> ul(mMutex);
68 mCondition.wait(ul, [this]() { return (mEnable || !mRunning); });
69 return mRunning;
70 }
71
72
WaitNextVBlank(unsigned int & sq)73 uint64_t DrmVsyncWorker::WaitNextVBlank(unsigned int &sq)
74 {
75 constexpr uint64_t SEC_TO_NSEC = 1000 * 1000 * 1000;
76 constexpr uint64_t USEC_TO_NSEC = 1000;
77 drmVBlank vblank = {
78 .request =
79 drmVBlankReq {
80 .type = DRM_VBLANK_RELATIVE,
81 .sequence = 1,
82 .signal = 0,
83 }
84 };
85 /* The drmWaitVBlank need set the crtc pipe when there are multi crtcs in the system. */
86 if (mCallBack->GetPipe() == 1)
87 vblank.request.type = drmVBlankSeqType((int)(vblank.request.type) | (int)DRM_VBLANK_SECONDARY);
88 int ret = drmWaitVBlank(mDrmFd, &vblank);
89 DISPLAY_CHK_RETURN((ret < 0), 0,
90 DISPLAY_LOGE("wait vblank failed ret : %{public}d errno %{public}d", ret, errno));
91 sq = vblank.reply.sequence;
92 return (uint64_t)(vblank.reply.tval_sec * SEC_TO_NSEC + vblank.reply.tval_usec * USEC_TO_NSEC);
93 }
94
95
EnableVsync(bool enable)96 void DrmVsyncWorker::EnableVsync(bool enable)
97 {
98 DISPLAY_LOGD();
99 {
100 std::lock_guard<std::mutex> lg(mMutex);
101 mEnable = enable;
102 }
103 mCondition.notify_one();
104 }
105
WorkThread()106 void DrmVsyncWorker::WorkThread()
107 {
108 DISPLAY_LOGD();
109 unsigned int seq = 0;
110 while (WaitSignalAndCheckRuning()) {
111 // wait the vblank
112 uint64_t time = WaitNextVBlank(seq);
113 if (mCallBack != nullptr) {
114 mCallBack->Vsync(seq, time);
115 } else {
116 DISPLAY_LOGE("the callbac is nullptr");
117 }
118 }
119 }
120
ReqesterVBlankCb(std::shared_ptr<VsyncCallBack> & cb)121 void DrmVsyncWorker::ReqesterVBlankCb(std::shared_ptr<VsyncCallBack> &cb)
122 {
123 DISPLAY_LOGD();
124 DISPLAY_CHK_RETURN_NOT_VALUE((cb == nullptr), DISPLAY_LOGE("the VBlankCallback is nullptr "));
125 mCallBack = cb;
126 }
127 } // namespace OHOS
128 } // namespace HDI
129 } // namespace DISPLAY
130