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 | |
Roman Stratiienko | bde9566 | 2022-12-10 20:27:58 +0200 | [diff] [blame] | 17 | #pragma once |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 18 | |
Roman Stratiienko | d2cc738 | 2022-12-28 18:51:59 +0200 | [diff] [blame] | 19 | #include <condition_variable> |
Roman Stratiienko | 863a3c2 | 2021-09-29 13:00:29 +0300 | [diff] [blame] | 20 | #include <functional> |
Roman Stratiienko | aa3cd54 | 2020-08-29 11:26:16 +0300 | [diff] [blame] | 21 | #include <map> |
Roman Stratiienko | d2cc738 | 2022-12-28 18:51:59 +0200 | [diff] [blame] | 22 | #include <mutex> |
| 23 | #include <thread> |
Roman Stratiienko | aa3cd54 | 2020-08-29 11:26:16 +0300 | [diff] [blame] | 24 | |
Roman Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 25 | #include "DrmDevice.h" |
Drew Davenport | 35a3957 | 2024-12-19 13:54:39 -0700 | [diff] [blame] | 26 | #include "utils/thread_annotations.h" |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
Roman Stratiienko | d2cc738 | 2022-12-28 18:51:59 +0200 | [diff] [blame] | 30 | class VSyncWorker { |
| 31 | public: |
Drew Davenport | 63a699e | 2024-12-13 15:00:00 -0700 | [diff] [blame] | 32 | using VsyncTimestampCallback = std::function<void(int64_t /*timestamp*/, |
| 33 | uint32_t /*period*/)>; |
| 34 | |
Drew Davenport | 30f4e9c | 2024-12-16 16:12:11 -0700 | [diff] [blame] | 35 | ~VSyncWorker(); |
Roman Stratiienko | d2cc738 | 2022-12-28 18:51:59 +0200 | [diff] [blame] | 36 | |
Drew Davenport | 15016c4 | 2024-12-13 15:13:28 -0700 | [diff] [blame] | 37 | auto static CreateInstance(std::shared_ptr<DrmDisplayPipeline> &pipe) |
Drew Davenport | 30f4e9c | 2024-12-16 16:12:11 -0700 | [diff] [blame] | 38 | -> std::unique_ptr<VSyncWorker>; |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 39 | |
Drew Davenport | bf711eb | 2025-02-19 09:41:20 -0700 | [diff] [blame] | 40 | // Set the expected vsync period. Resets internal timestamp tracking until the |
| 41 | // next vsync event is tracked. |
Drew Davenport | 5983318 | 2024-12-13 10:02:15 -0700 | [diff] [blame] | 42 | void SetVsyncPeriodNs(uint32_t vsync_period_ns); |
| 43 | |
Drew Davenport | 63a699e | 2024-12-13 15:00:00 -0700 | [diff] [blame] | 44 | // Set or clear a callback to be fired on vsync. |
| 45 | void SetTimestampCallback(std::optional<VsyncTimestampCallback> &&callback); |
| 46 | |
Drew Davenport | 33121b7 | 2024-12-13 14:59:35 -0700 | [diff] [blame] | 47 | // 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 | |
Drew Davenport | 609c353 | 2025-02-19 10:12:50 -0700 | [diff] [blame] | 53 | // Get the next predicted vsync timestamp after |time|, based on the last |
| 54 | // recorded vsync timestamp and the current vsync period. |
| 55 | int64_t GetNextVsyncTimestamp(int64_t time); |
| 56 | |
Roman Stratiienko | d2cc738 | 2022-12-28 18:51:59 +0200 | [diff] [blame] | 57 | void StopThread(); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 58 | |
| 59 | private: |
Roman Stratiienko | d2cc738 | 2022-12-28 18:51:59 +0200 | [diff] [blame] | 60 | VSyncWorker() = default; |
| 61 | |
Drew Davenport | 30f4e9c | 2024-12-16 16:12:11 -0700 | [diff] [blame] | 62 | void ThreadFn(); |
Roman Stratiienko | d2cc738 | 2022-12-28 18:51:59 +0200 | [diff] [blame] | 63 | |
Drew Davenport | 35a3957 | 2024-12-19 13:54:39 -0700 | [diff] [blame] | 64 | int64_t GetPhasedVSync(int64_t frame_ns, int64_t current) const |
| 65 | REQUIRES(mutex_); |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 66 | int SyntheticWaitVBlank(int64_t *timestamp); |
| 67 | |
Drew Davenport | b39a3f8 | 2024-12-13 15:04:56 -0700 | [diff] [blame] | 68 | void UpdateVSyncControl(); |
Drew Davenport | 35a3957 | 2024-12-19 13:54:39 -0700 | [diff] [blame] | 69 | bool ShouldEnable() const REQUIRES(mutex_); |
Drew Davenport | b39a3f8 | 2024-12-13 15:04:56 -0700 | [diff] [blame] | 70 | |
Roman Stratiienko | 7689278 | 2023-01-16 17:15:53 +0200 | [diff] [blame] | 71 | SharedFd drm_fd_; |
Roman Stratiienko | d2cc738 | 2022-12-28 18:51:59 +0200 | [diff] [blame] | 72 | uint32_t high_crtc_ = 0; |
| 73 | |
Drew Davenport | 35a3957 | 2024-12-19 13:54:39 -0700 | [diff] [blame] | 74 | bool enabled_ GUARDED_BY(mutex_) = false; |
| 75 | bool thread_exit_ GUARDED_BY(mutex_) = false; |
Drew Davenport | bf711eb | 2025-02-19 09:41:20 -0700 | [diff] [blame] | 76 | std::optional<int64_t> last_timestamp_ GUARDED_BY(mutex_); |
Roman Stratiienko | d2cc738 | 2022-12-28 18:51:59 +0200 | [diff] [blame] | 77 | |
Drew Davenport | d387c84 | 2024-12-16 16:57:24 -0700 | [diff] [blame] | 78 | // Default to 60Hz refresh rate |
| 79 | static constexpr uint32_t kDefaultVSPeriodNs = 16666666; |
Drew Davenport | 35a3957 | 2024-12-19 13:54:39 -0700 | [diff] [blame] | 80 | uint32_t vsync_period_ns_ GUARDED_BY(mutex_) = kDefaultVSPeriodNs; |
| 81 | bool enable_vsync_timestamps_ GUARDED_BY(mutex_) = false; |
Drew Davenport | 701a83c | 2025-02-19 09:51:28 -0700 | [diff] [blame] | 82 | bool last_timestamp_is_fresh_ GUARDED_BY(mutex_) = false; |
Drew Davenport | 35a3957 | 2024-12-19 13:54:39 -0700 | [diff] [blame] | 83 | std::optional<VsyncTimestampCallback> callback_ GUARDED_BY(mutex_); |
Drew Davenport | 5983318 | 2024-12-13 10:02:15 -0700 | [diff] [blame] | 84 | |
Roman Stratiienko | d2cc738 | 2022-12-28 18:51:59 +0200 | [diff] [blame] | 85 | std::condition_variable cv_; |
| 86 | std::thread vswt_; |
| 87 | std::mutex mutex_; |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 88 | }; |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 89 | } // namespace android |