Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 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 "hwc-vsync-worker" |
| 18 | |
Roman Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 19 | #include "VSyncWorker.h" |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 20 | |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <time.h> |
| 23 | #include <xf86drm.h> |
| 24 | #include <xf86drmMode.h> |
| 25 | |
Roman Stratiienko | d518a05 | 2021-02-25 19:15:14 +0200 | [diff] [blame^] | 26 | #include "utils/log.h" |
| 27 | |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | |
| 30 | VSyncWorker::VSyncWorker() |
| 31 | : Worker("vsync", HAL_PRIORITY_URGENT_DISPLAY), |
| 32 | drm_(NULL), |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 33 | display_(-1), |
Alexandru Gheorghe | 976f69a | 2018-04-11 16:22:12 +0100 | [diff] [blame] | 34 | enabled_(false), |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 35 | last_timestamp_(-1) { |
| 36 | } |
| 37 | |
| 38 | VSyncWorker::~VSyncWorker() { |
| 39 | } |
| 40 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 41 | int VSyncWorker::Init(DrmDevice *drm, int display) { |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 42 | drm_ = drm; |
| 43 | display_ = display; |
| 44 | |
| 45 | return InitWorker(); |
| 46 | } |
| 47 | |
Adrian Salido | fa37f67 | 2017-02-16 10:29:46 -0800 | [diff] [blame] | 48 | void VSyncWorker::RegisterCallback(std::shared_ptr<VsyncCallback> callback) { |
| 49 | Lock(); |
Sean Paul | a5df1de | 2016-02-10 10:00:08 -0800 | [diff] [blame] | 50 | callback_ = callback; |
Adrian Salido | fa37f67 | 2017-02-16 10:29:46 -0800 | [diff] [blame] | 51 | Unlock(); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Roman Stratiienko | 2370109 | 2020-09-26 02:08:41 +0300 | [diff] [blame] | 54 | void VSyncWorker::RegisterClientCallback(hwc2_callback_data_t data, |
| 55 | hwc2_function_pointer_t hook) { |
| 56 | Lock(); |
| 57 | vsync_callback_data_ = data; |
| 58 | vsync_callback_hook_ = reinterpret_cast<HWC2_PFN_VSYNC>(hook); |
| 59 | Unlock(); |
| 60 | } |
| 61 | |
Adrian Salido | fa37f67 | 2017-02-16 10:29:46 -0800 | [diff] [blame] | 62 | void VSyncWorker::VSyncControl(bool enabled) { |
| 63 | Lock(); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 64 | enabled_ = enabled; |
| 65 | last_timestamp_ = -1; |
Adrian Salido | fa37f67 | 2017-02-16 10:29:46 -0800 | [diff] [blame] | 66 | Unlock(); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 67 | |
Adrian Salido | fa37f67 | 2017-02-16 10:29:46 -0800 | [diff] [blame] | 68 | Signal(); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Returns the timestamp of the next vsync in phase with last_timestamp_. |
| 73 | * For example: |
| 74 | * last_timestamp_ = 137 |
| 75 | * frame_ns = 50 |
| 76 | * current = 683 |
| 77 | * |
| 78 | * ret = (50 * ((683 - 137)/50 + 1)) + 137 |
| 79 | * ret = 687 |
| 80 | * |
| 81 | * Thus, we must sleep until timestamp 687 to maintain phase with the last |
| 82 | * timestamp. |
| 83 | */ |
| 84 | int64_t VSyncWorker::GetPhasedVSync(int64_t frame_ns, int64_t current) { |
| 85 | if (last_timestamp_ < 0) |
| 86 | return current + frame_ns; |
| 87 | |
| 88 | return frame_ns * ((current - last_timestamp_) / frame_ns + 1) + |
| 89 | last_timestamp_; |
| 90 | } |
| 91 | |
| 92 | static const int64_t kOneSecondNs = 1 * 1000 * 1000 * 1000; |
| 93 | |
| 94 | int VSyncWorker::SyntheticWaitVBlank(int64_t *timestamp) { |
| 95 | struct timespec vsync; |
| 96 | int ret = clock_gettime(CLOCK_MONOTONIC, &vsync); |
| 97 | |
Stéphane Marchesin | b74e08c | 2015-07-05 02:00:08 -0700 | [diff] [blame] | 98 | float refresh = 60.0f; // Default to 60Hz refresh rate |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 99 | DrmConnector *conn = drm_->GetConnectorForDisplay(display_); |
Stéphane Marchesin | b74e08c | 2015-07-05 02:00:08 -0700 | [diff] [blame] | 100 | if (conn && conn->active_mode().v_refresh() != 0.0f) |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 101 | refresh = conn->active_mode().v_refresh(); |
| 102 | else |
Stéphane Marchesin | b74e08c | 2015-07-05 02:00:08 -0700 | [diff] [blame] | 103 | ALOGW("Vsync worker active with conn=%p refresh=%f\n", conn, |
| 104 | conn ? conn->active_mode().v_refresh() : 0.0f); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 105 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 106 | int64_t phased_timestamp = GetPhasedVSync(kOneSecondNs / refresh, |
| 107 | vsync.tv_sec * kOneSecondNs + |
| 108 | vsync.tv_nsec); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 109 | vsync.tv_sec = phased_timestamp / kOneSecondNs; |
| 110 | vsync.tv_nsec = phased_timestamp - (vsync.tv_sec * kOneSecondNs); |
| 111 | do { |
| 112 | ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &vsync, NULL); |
| 113 | } while (ret == -1 && errno == EINTR); |
| 114 | if (ret) |
| 115 | return ret; |
| 116 | |
| 117 | *timestamp = (int64_t)vsync.tv_sec * kOneSecondNs + (int64_t)vsync.tv_nsec; |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | void VSyncWorker::Routine() { |
Adrian Salido | fa37f67 | 2017-02-16 10:29:46 -0800 | [diff] [blame] | 122 | int ret; |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 123 | |
Adrian Salido | fa37f67 | 2017-02-16 10:29:46 -0800 | [diff] [blame] | 124 | Lock(); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 125 | if (!enabled_) { |
| 126 | ret = WaitForSignalOrExitLocked(); |
| 127 | if (ret == -EINTR) { |
Alexandru Gheorghe | acb6303 | 2018-03-27 14:09:17 +0100 | [diff] [blame] | 128 | Unlock(); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | } |
| 132 | |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 133 | int display = display_; |
Sean Paul | a5df1de | 2016-02-10 10:00:08 -0800 | [diff] [blame] | 134 | std::shared_ptr<VsyncCallback> callback(callback_); |
Adrian Salido | fa37f67 | 2017-02-16 10:29:46 -0800 | [diff] [blame] | 135 | Unlock(); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 136 | |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 137 | DrmCrtc *crtc = drm_->GetCrtcForDisplay(display); |
| 138 | if (!crtc) { |
| 139 | ALOGE("Failed to get crtc for display"); |
| 140 | return; |
| 141 | } |
| 142 | uint32_t high_crtc = (crtc->pipe() << DRM_VBLANK_HIGH_CRTC_SHIFT); |
| 143 | |
| 144 | drmVBlank vblank; |
| 145 | memset(&vblank, 0, sizeof(vblank)); |
| 146 | vblank.request.type = (drmVBlankSeqType)( |
| 147 | DRM_VBLANK_RELATIVE | (high_crtc & DRM_VBLANK_HIGH_CRTC_MASK)); |
| 148 | vblank.request.sequence = 1; |
| 149 | |
| 150 | int64_t timestamp; |
| 151 | ret = drmWaitVBlank(drm_->fd(), &vblank); |
| 152 | if (ret == -EINTR) { |
| 153 | return; |
| 154 | } else if (ret) { |
| 155 | ret = SyntheticWaitVBlank(×tamp); |
| 156 | if (ret) |
| 157 | return; |
| 158 | } else { |
| 159 | timestamp = (int64_t)vblank.reply.tval_sec * kOneSecondNs + |
| 160 | (int64_t)vblank.reply.tval_usec * 1000; |
| 161 | } |
| 162 | |
Roman Kovalivskyi | 521a4c8 | 2020-01-03 20:26:45 +0200 | [diff] [blame] | 163 | if (!enabled_) |
| 164 | return; |
Roman Stratiienko | 2370109 | 2020-09-26 02:08:41 +0300 | [diff] [blame] | 165 | |
Sean Paul | a5df1de | 2016-02-10 10:00:08 -0800 | [diff] [blame] | 166 | if (callback) |
| 167 | callback->Callback(display, timestamp); |
Roman Stratiienko | 2370109 | 2020-09-26 02:08:41 +0300 | [diff] [blame] | 168 | |
| 169 | Lock(); |
| 170 | if (enabled_ && vsync_callback_hook_ && vsync_callback_data_) |
| 171 | vsync_callback_hook_(vsync_callback_data_, display, timestamp); |
| 172 | Unlock(); |
| 173 | |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 174 | last_timestamp_ = timestamp; |
| 175 | } |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 176 | } // namespace android |