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 | |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [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 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 26 | #include <log/log.h> |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 27 | |
| 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 | |
Adrian Salido | fa37f67 | 2017-02-16 10:29:46 -0800 | [diff] [blame] | 54 | void VSyncWorker::VSyncControl(bool enabled) { |
| 55 | Lock(); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 56 | enabled_ = enabled; |
| 57 | last_timestamp_ = -1; |
Adrian Salido | fa37f67 | 2017-02-16 10:29:46 -0800 | [diff] [blame] | 58 | Unlock(); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 59 | |
Adrian Salido | fa37f67 | 2017-02-16 10:29:46 -0800 | [diff] [blame] | 60 | Signal(); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Returns the timestamp of the next vsync in phase with last_timestamp_. |
| 65 | * For example: |
| 66 | * last_timestamp_ = 137 |
| 67 | * frame_ns = 50 |
| 68 | * current = 683 |
| 69 | * |
| 70 | * ret = (50 * ((683 - 137)/50 + 1)) + 137 |
| 71 | * ret = 687 |
| 72 | * |
| 73 | * Thus, we must sleep until timestamp 687 to maintain phase with the last |
| 74 | * timestamp. |
| 75 | */ |
| 76 | int64_t VSyncWorker::GetPhasedVSync(int64_t frame_ns, int64_t current) { |
| 77 | if (last_timestamp_ < 0) |
| 78 | return current + frame_ns; |
| 79 | |
| 80 | return frame_ns * ((current - last_timestamp_) / frame_ns + 1) + |
| 81 | last_timestamp_; |
| 82 | } |
| 83 | |
| 84 | static const int64_t kOneSecondNs = 1 * 1000 * 1000 * 1000; |
| 85 | |
| 86 | int VSyncWorker::SyntheticWaitVBlank(int64_t *timestamp) { |
| 87 | struct timespec vsync; |
| 88 | int ret = clock_gettime(CLOCK_MONOTONIC, &vsync); |
| 89 | |
Stéphane Marchesin | b74e08c | 2015-07-05 02:00:08 -0700 | [diff] [blame] | 90 | float refresh = 60.0f; // Default to 60Hz refresh rate |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 91 | DrmConnector *conn = drm_->GetConnectorForDisplay(display_); |
Stéphane Marchesin | b74e08c | 2015-07-05 02:00:08 -0700 | [diff] [blame] | 92 | if (conn && conn->active_mode().v_refresh() != 0.0f) |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 93 | refresh = conn->active_mode().v_refresh(); |
| 94 | else |
Stéphane Marchesin | b74e08c | 2015-07-05 02:00:08 -0700 | [diff] [blame] | 95 | ALOGW("Vsync worker active with conn=%p refresh=%f\n", conn, |
| 96 | conn ? conn->active_mode().v_refresh() : 0.0f); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 97 | |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 98 | int64_t phased_timestamp = GetPhasedVSync(kOneSecondNs / refresh, |
| 99 | vsync.tv_sec * kOneSecondNs + |
| 100 | vsync.tv_nsec); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 101 | vsync.tv_sec = phased_timestamp / kOneSecondNs; |
| 102 | vsync.tv_nsec = phased_timestamp - (vsync.tv_sec * kOneSecondNs); |
| 103 | do { |
| 104 | ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &vsync, NULL); |
| 105 | } while (ret == -1 && errno == EINTR); |
| 106 | if (ret) |
| 107 | return ret; |
| 108 | |
| 109 | *timestamp = (int64_t)vsync.tv_sec * kOneSecondNs + (int64_t)vsync.tv_nsec; |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | void VSyncWorker::Routine() { |
Adrian Salido | fa37f67 | 2017-02-16 10:29:46 -0800 | [diff] [blame] | 114 | int ret; |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 115 | |
Adrian Salido | fa37f67 | 2017-02-16 10:29:46 -0800 | [diff] [blame] | 116 | Lock(); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 117 | if (!enabled_) { |
| 118 | ret = WaitForSignalOrExitLocked(); |
| 119 | if (ret == -EINTR) { |
Alexandru Gheorghe | acb6303 | 2018-03-27 14:09:17 +0100 | [diff] [blame] | 120 | Unlock(); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | } |
| 124 | |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 125 | int display = display_; |
Sean Paul | a5df1de | 2016-02-10 10:00:08 -0800 | [diff] [blame] | 126 | std::shared_ptr<VsyncCallback> callback(callback_); |
Adrian Salido | fa37f67 | 2017-02-16 10:29:46 -0800 | [diff] [blame] | 127 | Unlock(); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 128 | |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 129 | DrmCrtc *crtc = drm_->GetCrtcForDisplay(display); |
| 130 | if (!crtc) { |
| 131 | ALOGE("Failed to get crtc for display"); |
| 132 | return; |
| 133 | } |
| 134 | uint32_t high_crtc = (crtc->pipe() << DRM_VBLANK_HIGH_CRTC_SHIFT); |
| 135 | |
| 136 | drmVBlank vblank; |
| 137 | memset(&vblank, 0, sizeof(vblank)); |
| 138 | vblank.request.type = (drmVBlankSeqType)( |
| 139 | DRM_VBLANK_RELATIVE | (high_crtc & DRM_VBLANK_HIGH_CRTC_MASK)); |
| 140 | vblank.request.sequence = 1; |
| 141 | |
| 142 | int64_t timestamp; |
| 143 | ret = drmWaitVBlank(drm_->fd(), &vblank); |
| 144 | if (ret == -EINTR) { |
| 145 | return; |
| 146 | } else if (ret) { |
| 147 | ret = SyntheticWaitVBlank(×tamp); |
| 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 | |
| 155 | /* |
Roman Kovalivskyi | 521a4c8 | 2020-01-03 20:26:45 +0200 | [diff] [blame] | 156 | * VSync could be disabled during routine execution so it could potentially |
| 157 | * lead to crash since callback's inner hook could be invalid anymore. We have |
| 158 | * no control over lifetime of this hook, therefore we can't rely that it'll |
| 159 | * be valid after vsync disabling. |
| 160 | * |
| 161 | * Blocking VSyncControl to wait until routine |
| 162 | * will finish execution is logically correct way to fix this issue, but it |
| 163 | * creates visible lags and stutters, so we have to resort to other ways of |
| 164 | * mitigating this issue. |
| 165 | * |
| 166 | * Doing check before attempt to invoke callback drastically shortens the |
| 167 | * window when such situation could happen and that allows us to practically |
| 168 | * avoid this issue. |
| 169 | * |
| 170 | * Please note that issue described below is different one and it is related |
| 171 | * to RegisterCallback, not to disabling vsync via VSyncControl. |
| 172 | */ |
| 173 | if (!enabled_) |
| 174 | return; |
| 175 | /* |
Sean Paul | a5df1de | 2016-02-10 10:00:08 -0800 | [diff] [blame] | 176 | * There's a race here where a change in callback_ will not take effect until |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 177 | * the next subsequent requested vsync. This is unavoidable since we can't |
| 178 | * call the vsync hook while holding the thread lock. |
| 179 | * |
Sean Paul | a5df1de | 2016-02-10 10:00:08 -0800 | [diff] [blame] | 180 | * We could shorten the race window by caching callback_ right before calling |
| 181 | * the hook. However, in practice, callback_ is only updated once, so it's not |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 182 | * worth the overhead. |
| 183 | */ |
Sean Paul | a5df1de | 2016-02-10 10:00:08 -0800 | [diff] [blame] | 184 | if (callback) |
| 185 | callback->Callback(display, timestamp); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 186 | last_timestamp_ = timestamp; |
| 187 | } |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 188 | } // namespace android |