xref: /aosp_15_r20/external/drm_hwcomposer/drm/VSyncWorker.cpp (revision 0a9764fe0a15e71ebbeb85e87e10990c23aab47f)
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "drmhwc"
18 
19 #include "VSyncWorker.h"
20 
21 #include <xf86drm.h>
22 #include <xf86drmMode.h>
23 
24 #include <cstdlib>
25 #include <cstring>
26 #include <ctime>
27 
28 #include "drm/ResourceManager.h"
29 #include "utils/log.h"
30 
31 namespace android {
32 
CreateInstance(std::shared_ptr<DrmDisplayPipeline> & pipe,VSyncWorkerCallbacks & callbacks)33 auto VSyncWorker::CreateInstance(std::shared_ptr<DrmDisplayPipeline> &pipe,
34                                  VSyncWorkerCallbacks &callbacks)
35     -> std::shared_ptr<VSyncWorker> {
36   auto vsw = std::shared_ptr<VSyncWorker>(new VSyncWorker());
37 
38   vsw->callbacks_ = callbacks;
39 
40   if (pipe) {
41     vsw->high_crtc_ = pipe->crtc->Get()->GetIndexInResArray()
42                       << DRM_VBLANK_HIGH_CRTC_SHIFT;
43     vsw->drm_fd_ = pipe->device->GetFd();
44   }
45 
46   std::thread(&VSyncWorker::ThreadFn, vsw.get(), vsw).detach();
47 
48   return vsw;
49 }
50 
VSyncControl(bool enabled)51 void VSyncWorker::VSyncControl(bool enabled) {
52   {
53     const std::lock_guard<std::mutex> lock(mutex_);
54     enabled_ = enabled;
55     last_timestamp_ = -1;
56   }
57 
58   cv_.notify_all();
59 }
60 
StopThread()61 void VSyncWorker::StopThread() {
62   {
63     const std::lock_guard<std::mutex> lock(mutex_);
64     thread_exit_ = true;
65     enabled_ = false;
66     callbacks_ = {};
67   }
68 
69   cv_.notify_all();
70 }
71 
72 /*
73  * Returns the timestamp of the next vsync in phase with last_timestamp_.
74  * For example:
75  *  last_timestamp_ = 137
76  *  frame_ns = 50
77  *  current = 683
78  *
79  *  ret = (50 * ((683 - 137)/50 + 1)) + 137
80  *  ret = 687
81  *
82  *  Thus, we must sleep until timestamp 687 to maintain phase with the last
83  *  timestamp.
84  */
GetPhasedVSync(int64_t frame_ns,int64_t current) const85 int64_t VSyncWorker::GetPhasedVSync(int64_t frame_ns, int64_t current) const {
86   if (last_timestamp_ < 0)
87     return current + frame_ns;
88 
89   return frame_ns * ((current - last_timestamp_) / frame_ns + 1) +
90          last_timestamp_;
91 }
92 
93 static const int64_t kOneSecondNs = 1LL * 1000 * 1000 * 1000;
94 
SyntheticWaitVBlank(int64_t * timestamp)95 int VSyncWorker::SyntheticWaitVBlank(int64_t *timestamp) {
96   auto time_now = ResourceManager::GetTimeMonotonicNs();
97 
98   // Default to 60Hz refresh rate
99   constexpr uint32_t kDefaultVSPeriodNs = 16666666;
100   auto period_ns = kDefaultVSPeriodNs;
101   if (callbacks_.get_vperiod_ns && callbacks_.get_vperiod_ns() != 0)
102     period_ns = callbacks_.get_vperiod_ns();
103 
104   auto phased_timestamp = GetPhasedVSync(period_ns, time_now);
105   struct timespec vsync {};
106   vsync.tv_sec = int(phased_timestamp / kOneSecondNs);
107   vsync.tv_nsec = int(phased_timestamp - (vsync.tv_sec * kOneSecondNs));
108 
109   int ret = 0;
110   do {
111     ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &vsync, nullptr);
112   } while (ret == EINTR);
113   if (ret != 0)
114     return ret;
115 
116   *timestamp = phased_timestamp;
117   return 0;
118 }
119 
ThreadFn(const std::shared_ptr<VSyncWorker> & vsw)120 void VSyncWorker::ThreadFn(const std::shared_ptr<VSyncWorker> &vsw) {
121   int ret = 0;
122 
123   for (;;) {
124     {
125       std::unique_lock<std::mutex> lock(vsw->mutex_);
126       if (thread_exit_)
127         break;
128 
129       if (!enabled_)
130         vsw->cv_.wait(lock);
131 
132       if (!enabled_)
133         continue;
134     }
135 
136     ret = -EAGAIN;
137     int64_t timestamp = 0;
138     drmVBlank vblank{};
139 
140     if (drm_fd_) {
141       vblank.request.type = (drmVBlankSeqType)(DRM_VBLANK_RELATIVE |
142                                                (high_crtc_ &
143                                                 DRM_VBLANK_HIGH_CRTC_MASK));
144       vblank.request.sequence = 1;
145 
146       ret = drmWaitVBlank(*drm_fd_, &vblank);
147       if (ret == -EINTR)
148         continue;
149     }
150 
151     if (ret != 0) {
152       ret = SyntheticWaitVBlank(&timestamp);
153       if (ret != 0)
154         continue;
155     } else {
156       constexpr int kUsToNsMul = 1000;
157       timestamp = (int64_t)vblank.reply.tval_sec * kOneSecondNs +
158                   (int64_t)vblank.reply.tval_usec * kUsToNsMul;
159     }
160 
161     decltype(callbacks_.out_event) callback;
162 
163     {
164       const std::lock_guard<std::mutex> lock(mutex_);
165       if (!enabled_)
166         continue;
167       callback = callbacks_.out_event;
168     }
169 
170     if (callback)
171       callback(timestamp);
172 
173     last_timestamp_ = timestamp;
174   }
175 
176   ALOGI("VSyncWorker thread exit");
177 }
178 }  // namespace android
179