2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "hwc-vsync-worker"
19 #include "drmresources.h"
20 #include "vsyncworker.h"
27 #include <xf86drmMode.h>
29 #include <cutils/log.h>
30 #include <hardware/hardware.h>
34 VSyncWorker::VSyncWorker()
35 : Worker("vsync", HAL_PRIORITY_URGENT_DISPLAY),
41 VSyncWorker::~VSyncWorker() {
44 int VSyncWorker::Init(DrmResources *drm, int display) {
51 void VSyncWorker::RegisterCallback(std::shared_ptr<VsyncCallback> callback) {
57 void VSyncWorker::VSyncControl(bool enabled) {
67 * Returns the timestamp of the next vsync in phase with last_timestamp_.
69 * last_timestamp_ = 137
73 * ret = (50 * ((683 - 137)/50 + 1)) + 137
76 * Thus, we must sleep until timestamp 687 to maintain phase with the last
79 int64_t VSyncWorker::GetPhasedVSync(int64_t frame_ns, int64_t current) {
80 if (last_timestamp_ < 0)
81 return current + frame_ns;
83 return frame_ns * ((current - last_timestamp_) / frame_ns + 1) +
87 static const int64_t kOneSecondNs = 1 * 1000 * 1000 * 1000;
89 int VSyncWorker::SyntheticWaitVBlank(int64_t *timestamp) {
90 struct timespec vsync;
91 int ret = clock_gettime(CLOCK_MONOTONIC, &vsync);
93 float refresh = 60.0f; // Default to 60Hz refresh rate
94 DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
95 if (conn && conn->active_mode().v_refresh() != 0.0f)
96 refresh = conn->active_mode().v_refresh();
98 ALOGW("Vsync worker active with conn=%p refresh=%f\n", conn,
99 conn ? conn->active_mode().v_refresh() : 0.0f);
101 int64_t phased_timestamp = GetPhasedVSync(
102 kOneSecondNs / refresh, vsync.tv_sec * kOneSecondNs + vsync.tv_nsec);
103 vsync.tv_sec = phased_timestamp / kOneSecondNs;
104 vsync.tv_nsec = phased_timestamp - (vsync.tv_sec * kOneSecondNs);
106 ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &vsync, NULL);
107 } while (ret == -1 && errno == EINTR);
111 *timestamp = (int64_t)vsync.tv_sec * kOneSecondNs + (int64_t)vsync.tv_nsec;
115 void VSyncWorker::Routine() {
120 ret = WaitForSignalOrExitLocked();
126 bool enabled = enabled_;
127 int display = display_;
128 std::shared_ptr<VsyncCallback> callback(callback_);
134 DrmCrtc *crtc = drm_->GetCrtcForDisplay(display);
136 ALOGE("Failed to get crtc for display");
139 uint32_t high_crtc = (crtc->pipe() << DRM_VBLANK_HIGH_CRTC_SHIFT);
142 memset(&vblank, 0, sizeof(vblank));
143 vblank.request.type = (drmVBlankSeqType)(
144 DRM_VBLANK_RELATIVE | (high_crtc & DRM_VBLANK_HIGH_CRTC_MASK));
145 vblank.request.sequence = 1;
148 ret = drmWaitVBlank(drm_->fd(), &vblank);
152 ret = SyntheticWaitVBlank(×tamp);
156 timestamp = (int64_t)vblank.reply.tval_sec * kOneSecondNs +
157 (int64_t)vblank.reply.tval_usec * 1000;
161 * There's a race here where a change in callback_ will not take effect until
162 * the next subsequent requested vsync. This is unavoidable since we can't
163 * call the vsync hook while holding the thread lock.
165 * We could shorten the race window by caching callback_ right before calling
166 * the hook. However, in practice, callback_ is only updated once, so it's not
167 * worth the overhead.
170 callback->Callback(display, timestamp);
171 last_timestamp_ = timestamp;