Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
hermes_kms_capture.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <algorithm>
8#include <cerrno>
9#include <chrono>
10#include <cstdint>
11#include <limits>
12
13namespace VDISPLAY::hermes_kms {
14
15 constexpr uint64_t status_output_enabled = 1ULL << 0;
16 constexpr uint64_t status_connected = 1ULL << 1;
17 constexpr uint64_t status_scanout_active = 1ULL << 2;
18 constexpr uint64_t status_frame_valid = 1ULL << 3;
19 constexpr uint64_t status_dmabuf_export_ready = 1ULL << 4;
20
21 struct status_t {
22 uint64_t flags;
23 uint64_t frame_sequence;
24 uint64_t last_update_ns;
25 uint64_t last_enable_ns;
26 uint64_t last_disable_ns;
27 uint32_t connector_id;
28 uint32_t crtc_id;
29 uint32_t plane_id;
30 uint32_t encoder_id;
31 uint32_t requested_width;
32 uint32_t requested_height;
33 uint32_t requested_refresh_hz;
34 uint32_t active_width;
35 uint32_t active_height;
36 uint32_t active_refresh_hz;
37 uint32_t framebuffer_id;
38 uint32_t framebuffer_width;
39 uint32_t framebuffer_height;
40 uint32_t framebuffer_format;
41 uint32_t framebuffer_plane_count;
42 uint32_t framebuffer_pitch[4];
43 uint32_t framebuffer_offset[4];
44 uint32_t reserved_alignment;
45 uint64_t framebuffer_modifier;
46 uint64_t session_id;
47 int32_t owner_pid;
48 uint32_t reserved0;
52 uint64_t reserved[5];
53 };
54
61 template<class Query, class Now, class SleepUntil>
62 int wait_for_scanout(int &width, int &height, std::chrono::milliseconds timeout, Query query, Now now, SleepUntil sleep_until) {
63 using namespace std::chrono_literals;
64 width = height = 0;
65 const auto deadline = now() + timeout;
66 constexpr auto ready_flags = status_output_enabled | status_connected |
67 status_scanout_active | status_frame_valid |
68 status_dmabuf_export_ready;
69 for (;;) {
70 status_t status {};
71 const int error = query(status);
72 if (error == 0) {
73 if ((status.flags & ready_flags) == ready_flags && status.active_width && status.active_height && status.framebuffer_width && status.framebuffer_height) {
74 if (status.active_width > static_cast<uint32_t>(std::numeric_limits<int>::max()) || status.active_height > static_cast<uint32_t>(std::numeric_limits<int>::max())) {
75 return EOVERFLOW;
76 }
77 width = static_cast<int>(status.active_width);
78 height = static_cast<int>(status.active_height);
79 return 0;
80 }
81 } else if (error != EINTR && error != EAGAIN && error != ENODATA) {
82 // In particular EACCES is not a compositor delay: never hide a failed
83 // session binding behind a timeout or a missing-geometry diagnostic.
84 return error;
85 }
86 if (timeout == 0ms) {
87 return error ? error : EAGAIN;
88 }
89 const auto current = now();
90 if (current >= deadline) {
91 return ETIMEDOUT;
92 }
93 sleep_until(std::min(deadline, current + 50ms));
94 }
95 }
96
97} // namespace VDISPLAY::hermes_kms
int wait_for_scanout(int &width, int &height, std::chrono::milliseconds timeout, Query query, Now now, SleepUntil sleep_until)
@ current
KWin already drives the output at that mode.