blob: f1143e4dbc9a23a2f712aec48c164d330e6537ae [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"
Sean Paul4057be32015-05-13 06:23:09 -070026
27namespace android {
28
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020029struct VSyncWorkerCallbacks {
30 std::function<void(uint64_t /*timestamp*/)> out_event;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020031};
Sean Paul4057be32015-05-13 06:23:09 -070032
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020033class VSyncWorker {
34 public:
Drew Davenport63a699e2024-12-13 15:00:00 -070035 using VsyncTimestampCallback = std::function<void(int64_t /*timestamp*/,
36 uint32_t /*period*/)>;
37
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020038 ~VSyncWorker() = default;
39
Roman Stratiienko63762a92023-09-18 22:33:45 +030040 auto static CreateInstance(std::shared_ptr<DrmDisplayPipeline> &pipe,
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020041 VSyncWorkerCallbacks &callbacks)
42 -> std::shared_ptr<VSyncWorker>;
Sean Paul4057be32015-05-13 06:23:09 -070043
Drew Davenport59833182024-12-13 10:02:15 -070044 // Set the expected vsync period.
45 void SetVsyncPeriodNs(uint32_t vsync_period_ns);
46
Drew Davenport63a699e2024-12-13 15:00:00 -070047 // Set or clear a callback to be fired on vsync.
48 void SetTimestampCallback(std::optional<VsyncTimestampCallback> &&callback);
49
Drew Davenport33121b72024-12-13 14:59:35 -070050 // Enable vsync timestamp tracking. GetLastVsyncTimestamp will return 0 if
51 // vsync tracking is disabled, or if no vsync has happened since it was
52 // enabled.
53 void SetVsyncTimestampTracking(bool enabled);
54 uint32_t GetLastVsyncTimestamp();
55
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020056 void StopThread();
Sean Paul4057be32015-05-13 06:23:09 -070057
58 private:
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020059 VSyncWorker() = default;
60
61 void ThreadFn(const std::shared_ptr<VSyncWorker> &vsw);
62
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020063 int64_t GetPhasedVSync(int64_t frame_ns, int64_t current) const;
Sean Paul4057be32015-05-13 06:23:09 -070064 int SyntheticWaitVBlank(int64_t *timestamp);
65
Drew Davenportb39a3f82024-12-13 15:04:56 -070066 // Must hold the lock before calling these.
67 void UpdateVSyncControl();
68 bool ShouldEnable() const;
69
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020070 VSyncWorkerCallbacks callbacks_;
Sean Paul4057be32015-05-13 06:23:09 -070071
Roman Stratiienko76892782023-01-16 17:15:53 +020072 SharedFd drm_fd_;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020073 uint32_t high_crtc_ = 0;
74
75 bool enabled_ = false;
76 bool thread_exit_ = false;
Roman Stratiienko19c162f2022-02-01 09:35:08 +020077 int64_t last_timestamp_ = -1;
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020078
Drew Davenportd387c842024-12-16 16:57:24 -070079 // Default to 60Hz refresh rate
80 static constexpr uint32_t kDefaultVSPeriodNs = 16666666;
Drew Davenport59833182024-12-13 10:02:15 -070081 // Needs to be threadsafe.
Drew Davenportd387c842024-12-16 16:57:24 -070082 uint32_t vsync_period_ns_ = kDefaultVSPeriodNs;
Drew Davenport33121b72024-12-13 14:59:35 -070083 bool enable_vsync_timestamps_ = false;
84 uint32_t last_vsync_timestamp_ = 0;
Drew Davenport63a699e2024-12-13 15:00:00 -070085 std::optional<VsyncTimestampCallback> callback_;
Drew Davenport59833182024-12-13 10:02:15 -070086
Roman Stratiienkod2cc7382022-12-28 18:51:59 +020087 std::condition_variable cv_;
88 std::thread vswt_;
89 std::mutex mutex_;
Sean Paul4057be32015-05-13 06:23:09 -070090};
Sean Paulf72cccd2018-08-27 13:59:08 -040091} // namespace android