blob: ed41189e1c063e77314caebc4d990ff231164484 [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
Roman Stratiienko19c162f2022-02-01 09:35:08 +020032VSyncWorker::VSyncWorker() : Worker("vsync", HAL_PRIORITY_URGENT_DISPLAY){};
Sean Paul4057be32015-05-13 06:23:09 -070033
Roman Stratiienko19c162f2022-02-01 09:35:08 +020034auto VSyncWorker::Init(DrmDisplayPipeline *pipe,
Roman Stratiienko863a3c22021-09-29 13:00:29 +030035 std::function<void(uint64_t /*timestamp*/)> callback)
36 -> int {
Roman Stratiienko19c162f2022-02-01 09:35:08 +020037 pipe_ = pipe;
Roman Stratiienko863a3c22021-09-29 13:00:29 +030038 callback_ = std::move(callback);
Sean Paul4057be32015-05-13 06:23:09 -070039
40 return InitWorker();
41}
42
Adrian Salidofa37f672017-02-16 10:29:46 -080043void VSyncWorker::VSyncControl(bool enabled) {
44 Lock();
Sean Paul4057be32015-05-13 06:23:09 -070045 enabled_ = enabled;
46 last_timestamp_ = -1;
Adrian Salidofa37f672017-02-16 10:29:46 -080047 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -070048
Adrian Salidofa37f672017-02-16 10:29:46 -080049 Signal();
Sean Paul4057be32015-05-13 06:23:09 -070050}
51
52/*
53 * Returns the timestamp of the next vsync in phase with last_timestamp_.
54 * For example:
55 * last_timestamp_ = 137
56 * frame_ns = 50
57 * current = 683
58 *
59 * ret = (50 * ((683 - 137)/50 + 1)) + 137
60 * ret = 687
61 *
62 * Thus, we must sleep until timestamp 687 to maintain phase with the last
63 * timestamp.
64 */
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020065int64_t VSyncWorker::GetPhasedVSync(int64_t frame_ns, int64_t current) const {
Sean Paul4057be32015-05-13 06:23:09 -070066 if (last_timestamp_ < 0)
67 return current + frame_ns;
68
69 return frame_ns * ((current - last_timestamp_) / frame_ns + 1) +
70 last_timestamp_;
71}
72
Roman Stratiienko780f7da2022-01-10 16:04:15 +020073static const int64_t kOneSecondNs = 1LL * 1000 * 1000 * 1000;
Sean Paul4057be32015-05-13 06:23:09 -070074
75int VSyncWorker::SyntheticWaitVBlank(int64_t *timestamp) {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020076 struct timespec vsync {};
Sean Paul4057be32015-05-13 06:23:09 -070077 int ret = clock_gettime(CLOCK_MONOTONIC, &vsync);
Roman Stratiienko6a10c4c2021-02-15 11:25:23 +020078 if (ret)
79 return ret;
Sean Paul4057be32015-05-13 06:23:09 -070080
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020081 float refresh = 60.0F; // Default to 60Hz refresh rate
Roman Stratiienko19c162f2022-02-01 09:35:08 +020082 if (pipe_ != nullptr &&
83 pipe_->connector->Get()->GetActiveMode().v_refresh() != 0.0F) {
84 refresh = pipe_->connector->Get()->GetActiveMode().v_refresh();
85 }
Sean Paul4057be32015-05-13 06:23:09 -070086
Roman Stratiienkod26619b2021-08-04 19:55:37 +030087 int64_t phased_timestamp = GetPhasedVSync(kOneSecondNs /
88 static_cast<int>(refresh),
Sean Paulf72cccd2018-08-27 13:59:08 -040089 vsync.tv_sec * kOneSecondNs +
90 vsync.tv_nsec);
Sean Paul4057be32015-05-13 06:23:09 -070091 vsync.tv_sec = phased_timestamp / kOneSecondNs;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020092 vsync.tv_nsec = int(phased_timestamp - (vsync.tv_sec * kOneSecondNs));
Sean Paul4057be32015-05-13 06:23:09 -070093 do {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020094 ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &vsync, nullptr);
Keith Mok554743d2021-11-17 01:09:44 +000095 } while (ret == EINTR);
Sean Paul4057be32015-05-13 06:23:09 -070096 if (ret)
97 return ret;
98
99 *timestamp = (int64_t)vsync.tv_sec * kOneSecondNs + (int64_t)vsync.tv_nsec;
100 return 0;
101}
102
103void VSyncWorker::Routine() {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200104 int ret = 0;
Sean Paul4057be32015-05-13 06:23:09 -0700105
Adrian Salidofa37f672017-02-16 10:29:46 -0800106 Lock();
Sean Paul4057be32015-05-13 06:23:09 -0700107 if (!enabled_) {
108 ret = WaitForSignalOrExitLocked();
109 if (ret == -EINTR) {
Alexandru Gheorgheacb63032018-03-27 14:09:17 +0100110 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -0700111 return;
112 }
113 }
114
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200115 auto *pipe = pipe_;
Adrian Salidofa37f672017-02-16 10:29:46 -0800116 Unlock();
Sean Paul4057be32015-05-13 06:23:09 -0700117
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200118 ret = -EAGAIN;
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200119 int64_t timestamp = 0;
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200120 drmVBlank vblank{};
121
122 if (pipe != nullptr) {
123 uint32_t high_crtc = (pipe->crtc->Get()->GetIndexInResArray()
124 << DRM_VBLANK_HIGH_CRTC_SHIFT);
125
126 vblank.request.type = (drmVBlankSeqType)(DRM_VBLANK_RELATIVE |
127 (high_crtc &
128 DRM_VBLANK_HIGH_CRTC_MASK));
129 vblank.request.sequence = 1;
130
131 ret = drmWaitVBlank(pipe->device->GetFd(), &vblank);
132 if (ret == -EINTR)
133 return;
134 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200135
136 if (ret) {
Sean Paul4057be32015-05-13 06:23:09 -0700137 ret = SyntheticWaitVBlank(&timestamp);
138 if (ret)
139 return;
140 } else {
141 timestamp = (int64_t)vblank.reply.tval_sec * kOneSecondNs +
142 (int64_t)vblank.reply.tval_usec * 1000;
143 }
144
Roman Kovalivskyi521a4c82020-01-03 20:26:45 +0200145 if (!enabled_)
146 return;
Roman Stratiienko23701092020-09-26 02:08:41 +0300147
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300148 if (callback_) {
149 callback_(timestamp);
150 }
Roman Stratiienko23701092020-09-26 02:08:41 +0300151
Sean Paul4057be32015-05-13 06:23:09 -0700152 last_timestamp_ = timestamp;
153}
Sean Paulf72cccd2018-08-27 13:59:08 -0400154} // namespace android