blob: 8d1cb99ee6a9f7df2090869883fdd3a0c5317bbb [file] [log] [blame]
Sean Paul4057be32015-05-13 06:23:09 -07001/*
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 "hwc-vsync-worker"
18
Roman Stratiienko13cc3662020-08-29 21:35:39 +030019#include "VSyncWorker.h"
Sean Paul4057be32015-05-13 06:23:09 -070020
Sean Paul4057be32015-05-13 06:23:09 -070021#include <xf86drm.h>
22#include <xf86drmMode.h>
23
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020024#include <cstdlib>
Roman Stratiienkod21071f2021-03-09 21:56:50 +020025#include <cstring>
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020026#include <ctime>
27
Roman Stratiienkod518a052021-02-25 19:15:14 +020028#include "utils/log.h"
29
Sean Paul4057be32015-05-13 06:23:09 -070030namespace android {
31
32VSyncWorker::VSyncWorker()
33 : Worker("vsync", HAL_PRIORITY_URGENT_DISPLAY),
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020034 drm_(nullptr),
Sean Paul4057be32015-05-13 06:23:09 -070035 display_(-1),
Alexandru Gheorghe976f69a2018-04-11 16:22:12 +010036 enabled_(false),
Sean Paul4057be32015-05-13 06:23:09 -070037 last_timestamp_(-1) {
38}
39
Roman Stratiienko863a3c22021-09-29 13:00:29 +030040auto VSyncWorker::Init(DrmDevice *drm, int display,
41 std::function<void(uint64_t /*timestamp*/)> callback)
42 -> int {
Sean Paul4057be32015-05-13 06:23:09 -070043 drm_ = drm;
44 display_ = display;
Roman Stratiienko863a3c22021-09-29 13:00:29 +030045 callback_ = std::move(callback);
Sean Paul4057be32015-05-13 06:23:09 -070046
47 return InitWorker();
48}
49
Adrian Salidofa37f672017-02-16 10:29:46 -080050void VSyncWorker::VSyncControl(bool enabled) {
51 Lock();
Sean Paul4057be32015-05-13 06:23:09 -070052 enabled_ = enabled;
53 last_timestamp_ = -1;
Adrian Salidofa37f672017-02-16 10:29:46 -080054 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -070055
Adrian Salidofa37f672017-02-16 10:29:46 -080056 Signal();
Sean Paul4057be32015-05-13 06:23:09 -070057}
58
59/*
60 * Returns the timestamp of the next vsync in phase with last_timestamp_.
61 * For example:
62 * last_timestamp_ = 137
63 * frame_ns = 50
64 * current = 683
65 *
66 * ret = (50 * ((683 - 137)/50 + 1)) + 137
67 * ret = 687
68 *
69 * Thus, we must sleep until timestamp 687 to maintain phase with the last
70 * timestamp.
71 */
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020072int64_t VSyncWorker::GetPhasedVSync(int64_t frame_ns, int64_t current) const {
Sean Paul4057be32015-05-13 06:23:09 -070073 if (last_timestamp_ < 0)
74 return current + frame_ns;
75
76 return frame_ns * ((current - last_timestamp_) / frame_ns + 1) +
77 last_timestamp_;
78}
79
Roman Stratiienko780f7da2022-01-10 16:04:15 +020080static const int64_t kOneSecondNs = 1LL * 1000 * 1000 * 1000;
Sean Paul4057be32015-05-13 06:23:09 -070081
82int VSyncWorker::SyntheticWaitVBlank(int64_t *timestamp) {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020083 struct timespec vsync {};
Sean Paul4057be32015-05-13 06:23:09 -070084 int ret = clock_gettime(CLOCK_MONOTONIC, &vsync);
Roman Stratiienko6a10c4c2021-02-15 11:25:23 +020085 if (ret)
86 return ret;
Sean Paul4057be32015-05-13 06:23:09 -070087
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020088 float refresh = 60.0F; // Default to 60Hz refresh rate
Sean Paul4057be32015-05-13 06:23:09 -070089 DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020090 if (conn && conn->active_mode().v_refresh() != 0.0F)
Sean Paul4057be32015-05-13 06:23:09 -070091 refresh = conn->active_mode().v_refresh();
92 else
Stéphane Marchesinb74e08c2015-07-05 02:00:08 -070093 ALOGW("Vsync worker active with conn=%p refresh=%f\n", conn,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020094 conn ? conn->active_mode().v_refresh() : 0.0F);
Sean Paul4057be32015-05-13 06:23:09 -070095
Roman Stratiienkod26619b2021-08-04 19:55:37 +030096 int64_t phased_timestamp = GetPhasedVSync(kOneSecondNs /
97 static_cast<int>(refresh),
Sean Paulf72cccd2018-08-27 13:59:08 -040098 vsync.tv_sec * kOneSecondNs +
99 vsync.tv_nsec);
Sean Paul4057be32015-05-13 06:23:09 -0700100 vsync.tv_sec = phased_timestamp / kOneSecondNs;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200101 vsync.tv_nsec = int(phased_timestamp - (vsync.tv_sec * kOneSecondNs));
Sean Paul4057be32015-05-13 06:23:09 -0700102 do {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200103 ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &vsync, nullptr);
Sean Paul4057be32015-05-13 06:23:09 -0700104 } while (ret == -1 && errno == EINTR);
105 if (ret)
106 return ret;
107
108 *timestamp = (int64_t)vsync.tv_sec * kOneSecondNs + (int64_t)vsync.tv_nsec;
109 return 0;
110}
111
112void VSyncWorker::Routine() {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200113 int ret = 0;
Sean Paul4057be32015-05-13 06:23:09 -0700114
Adrian Salidofa37f672017-02-16 10:29:46 -0800115 Lock();
Sean Paul4057be32015-05-13 06:23:09 -0700116 if (!enabled_) {
117 ret = WaitForSignalOrExitLocked();
118 if (ret == -EINTR) {
Alexandru Gheorgheacb63032018-03-27 14:09:17 +0100119 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -0700120 return;
121 }
122 }
123
Sean Paul4057be32015-05-13 06:23:09 -0700124 int display = display_;
Adrian Salidofa37f672017-02-16 10:29:46 -0800125 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -0700126
Sean Paul4057be32015-05-13 06:23:09 -0700127 DrmCrtc *crtc = drm_->GetCrtcForDisplay(display);
128 if (!crtc) {
129 ALOGE("Failed to get crtc for display");
130 return;
131 }
132 uint32_t high_crtc = (crtc->pipe() << DRM_VBLANK_HIGH_CRTC_SHIFT);
133
134 drmVBlank vblank;
135 memset(&vblank, 0, sizeof(vblank));
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300136 vblank.request.type = (drmVBlankSeqType)(DRM_VBLANK_RELATIVE |
137 (high_crtc &
138 DRM_VBLANK_HIGH_CRTC_MASK));
Sean Paul4057be32015-05-13 06:23:09 -0700139 vblank.request.sequence = 1;
140
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200141 int64_t timestamp = 0;
Sean Paul4057be32015-05-13 06:23:09 -0700142 ret = drmWaitVBlank(drm_->fd(), &vblank);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200143 if (ret == -EINTR)
Sean Paul4057be32015-05-13 06:23:09 -0700144 return;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200145
146 if (ret) {
Sean Paul4057be32015-05-13 06:23:09 -0700147 ret = SyntheticWaitVBlank(&timestamp);
148 if (ret)
149 return;
150 } else {
151 timestamp = (int64_t)vblank.reply.tval_sec * kOneSecondNs +
152 (int64_t)vblank.reply.tval_usec * 1000;
153 }
154
Roman Kovalivskyi521a4c82020-01-03 20:26:45 +0200155 if (!enabled_)
156 return;
Roman Stratiienko23701092020-09-26 02:08:41 +0300157
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300158 if (callback_) {
159 callback_(timestamp);
160 }
Roman Stratiienko23701092020-09-26 02:08:41 +0300161
Sean Paul4057be32015-05-13 06:23:09 -0700162 last_timestamp_ = timestamp;
163}
Sean Paulf72cccd2018-08-27 13:59:08 -0400164} // namespace android