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