blob: 8e410f4fb41835ee0fba26de7dae01e80cab20a9 [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
Roman Stratiienkobde95662022-12-10 20:27:58 +020017#pragma once
Sean Paul4057be32015-05-13 06:23:09 -070018
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020019#include <condition_variable>
Roman Stratiienko863a3c22021-09-29 13:00:29 +030020#include <functional>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030021#include <map>
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020022#include <mutex>
23#include <thread>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030024
Roman Stratiienko13cc3662020-08-29 21:35:39 +030025#include "DrmDevice.h"
Drew Davenport35a39572024-12-19 13:54:39 -070026#include "utils/thread_annotations.h"
Sean Paul4057be32015-05-13 06:23:09 -070027
28namespace android {
29
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020030class VSyncWorker {
31 public:
Drew Davenport63a699e2024-12-13 15:00:00 -070032 using VsyncTimestampCallback = std::function<void(int64_t /*timestamp*/,
33 uint32_t /*period*/)>;
34
Drew Davenport30f4e9c2024-12-16 16:12:11 -070035 ~VSyncWorker();
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020036
Drew Davenport15016c42024-12-13 15:13:28 -070037 auto static CreateInstance(std::shared_ptr<DrmDisplayPipeline> &pipe)
Drew Davenport30f4e9c2024-12-16 16:12:11 -070038 -> std::unique_ptr<VSyncWorker>;
Sean Paul4057be32015-05-13 06:23:09 -070039
Drew Davenportbf711eb2025-02-19 09:41:20 -070040 // Set the expected vsync period. Resets internal timestamp tracking until the
41 // next vsync event is tracked.
Drew Davenport59833182024-12-13 10:02:15 -070042 void SetVsyncPeriodNs(uint32_t vsync_period_ns);
43
Drew Davenport63a699e2024-12-13 15:00:00 -070044 // Set or clear a callback to be fired on vsync.
45 void SetTimestampCallback(std::optional<VsyncTimestampCallback> &&callback);
46
Drew Davenport33121b72024-12-13 14:59:35 -070047 // Enable vsync timestamp tracking. GetLastVsyncTimestamp will return 0 if
48 // vsync tracking is disabled, or if no vsync has happened since it was
49 // enabled.
50 void SetVsyncTimestampTracking(bool enabled);
51 uint32_t GetLastVsyncTimestamp();
52
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020053 void StopThread();
Sean Paul4057be32015-05-13 06:23:09 -070054
55 private:
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020056 VSyncWorker() = default;
57
Drew Davenport30f4e9c2024-12-16 16:12:11 -070058 void ThreadFn();
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020059
Drew Davenport35a39572024-12-19 13:54:39 -070060 int64_t GetPhasedVSync(int64_t frame_ns, int64_t current) const
61 REQUIRES(mutex_);
Sean Paul4057be32015-05-13 06:23:09 -070062 int SyntheticWaitVBlank(int64_t *timestamp);
63
Drew Davenportb39a3f82024-12-13 15:04:56 -070064 void UpdateVSyncControl();
Drew Davenport35a39572024-12-19 13:54:39 -070065 bool ShouldEnable() const REQUIRES(mutex_);
Drew Davenportb39a3f82024-12-13 15:04:56 -070066
Roman Stratiienko76892782023-01-16 17:15:53 +020067 SharedFd drm_fd_;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020068 uint32_t high_crtc_ = 0;
69
Drew Davenport35a39572024-12-19 13:54:39 -070070 bool enabled_ GUARDED_BY(mutex_) = false;
71 bool thread_exit_ GUARDED_BY(mutex_) = false;
Drew Davenportbf711eb2025-02-19 09:41:20 -070072 std::optional<int64_t> last_timestamp_ GUARDED_BY(mutex_);
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020073
Drew Davenportd387c842024-12-16 16:57:24 -070074 // Default to 60Hz refresh rate
75 static constexpr uint32_t kDefaultVSPeriodNs = 16666666;
Drew Davenport35a39572024-12-19 13:54:39 -070076 uint32_t vsync_period_ns_ GUARDED_BY(mutex_) = kDefaultVSPeriodNs;
77 bool enable_vsync_timestamps_ GUARDED_BY(mutex_) = false;
Drew Davenport701a83c2025-02-19 09:51:28 -070078 bool last_timestamp_is_fresh_ GUARDED_BY(mutex_) = false;
Drew Davenport35a39572024-12-19 13:54:39 -070079 std::optional<VsyncTimestampCallback> callback_ GUARDED_BY(mutex_);
Drew Davenport59833182024-12-13 10:02:15 -070080
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020081 std::condition_variable cv_;
82 std::thread vswt_;
83 std::mutex mutex_;
Sean Paul4057be32015-05-13 06:23:09 -070084};
Sean Paulf72cccd2018-08-27 13:59:08 -040085} // namespace android