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 "drm_vsync_worker.h"
17 #include <chrono>
18 #include "display_common.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     int ret = drmWaitVBlank(mDrmFd, &vblank);
86     DISPLAY_CHK_RETURN((ret < 0), 0,
87         DISPLAY_LOGE("wait vblank failed ret : %{public}d errno %{public}d", ret, errno));
88     sq = vblank.reply.sequence;
89     return static_cast<uint64_t>(vblank.reply.tval_sec * SEC_TO_NSEC + vblank.reply.tval_usec * USEC_TO_NSEC);
90 }
91 
92 
EnableVsync(bool enable)93 void DrmVsyncWorker::EnableVsync(bool enable)
94 {
95     DISPLAY_LOGD();
96     {
97         std::lock_guard<std::mutex> lg(mMutex);
98         mEnable = enable;
99     }
100     mCondition.notify_one();
101 }
102 
WorkThread()103 void DrmVsyncWorker::WorkThread()
104 {
105     DISPLAY_LOGD();
106     unsigned int seq = 0;
107     while (WaitSignalAndCheckRuning()) {
108         // wait the vblank
109         uint64_t time = WaitNextVBlank(seq);
110         if (mCallBack != nullptr) {
111             mCallBack->Vsync(seq, time);
112         } else {
113             DISPLAY_LOGE("the callbac is nullptr");
114         }
115     }
116 }
117 
ReqesterVBlankCb(const std::shared_ptr<VsyncCallBack> & cb)118 void DrmVsyncWorker::ReqesterVBlankCb(const std::shared_ptr<VsyncCallBack> &cb)
119 {
120     DISPLAY_LOGD();
121     DISPLAY_CHK_RETURN_NOT_VALUE((cb == nullptr), DISPLAY_LOGE("the VBlankCallback is nullptr "));
122     mCallBack = cb;
123 }
124 } // namespace OHOS
125 } // namespace HDI
126 } // namespace DISPLAY