Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
wayland.h
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <bitset>
9#include <cstdint>
10#include <map>
11#include <vector>
12
13#ifdef HERMES_BUILD_WAYLAND
14 #include <ext-image-capture-source-v1.h>
15 #include <ext-image-copy-capture-v1.h>
16 #include <linux-dmabuf-unstable-v1.h>
17 #include <wlr-output-management-unstable-v1.h>
18 #include <wlr-screencopy-unstable-v1.h>
19 #include <xdg-output-unstable-v1.h>
20#endif
21
22// local includes
23#include "graphics.h"
24
25// Declared at global scope so a translation unit that never includes <gbm.h>
26// agrees with one that does about the type of dmabuf_t's members; otherwise
27// `struct gbm_device *` below declares a wl::gbm_device of its own and LTO
28// reports the mismatch.
29struct gbm_device;
30struct gbm_bo;
31
36#ifdef HERMES_BUILD_WAYLAND
37
38namespace wl {
39 using display_internal_t = util::safe_ptr<wl_display, wl_display_disconnect>;
40
41 class frame_t {
42 public:
43 frame_t();
44 void destroy();
45
47 };
48
49 class dmabuf_t {
50 public:
56
57 dmabuf_t();
58 ~dmabuf_t();
59
60 dmabuf_t(dmabuf_t &&) = delete;
61 dmabuf_t(const dmabuf_t &) = delete;
62 dmabuf_t &operator=(const dmabuf_t &) = delete;
63 dmabuf_t &operator=(dmabuf_t &&) = delete;
64
65 void listen(zwlr_screencopy_manager_v1 *screencopy_manager, zwp_linux_dmabuf_v1 *dmabuf_interface, wl_output *output, bool blend_cursor = false);
70 void set_render_node(std::string node);
71 static void buffer_params_created(void *data, struct zwp_linux_buffer_params_v1 *params, struct wl_buffer *wl_buffer);
72 static void buffer_params_failed(void *data, struct zwp_linux_buffer_params_v1 *params);
73
83 bool icc_listen(ext_image_copy_capture_manager_v1 *manager, ext_output_image_capture_source_manager_v1 *source_manager, zwp_linux_dmabuf_v1 *dmabuf_interface, wl_output *output, bool blend_cursor = false);
84
86 void icc_capture(bool blend_cursor = false);
87
89 bool icc_ready() const {
90 return icc_session.session && icc_session.done;
91 }
92
94 bool icc_active() const {
95 return icc_session.session != nullptr;
96 }
97
98 void icc_buffer_params_created(zwp_linux_buffer_params_v1 *params, struct wl_buffer *buffer);
99 void icc_buffer_params_failed(zwp_linux_buffer_params_v1 *params);
100 void icc_session_buffer_size(ext_image_copy_capture_session_v1 *session, std::uint32_t width, std::uint32_t height);
101 void icc_session_shm_format(ext_image_copy_capture_session_v1 *session, std::uint32_t format);
102 void icc_session_dmabuf_device(ext_image_copy_capture_session_v1 *session, struct wl_array *device);
103 void icc_session_dmabuf_format(ext_image_copy_capture_session_v1 *session, std::uint32_t format, struct wl_array *modifiers);
104 void icc_session_done(ext_image_copy_capture_session_v1 *session);
105 void icc_session_stopped(ext_image_copy_capture_session_v1 *session);
106 void icc_frame_transform(ext_image_copy_capture_frame_v1 *frame, std::uint32_t transform);
107 void icc_frame_damage(ext_image_copy_capture_frame_v1 *frame, std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height);
108 void icc_frame_presentation_time(ext_image_copy_capture_frame_v1 *frame, std::uint32_t tv_sec_hi, std::uint32_t tv_sec_lo, std::uint32_t tv_nsec);
109 void icc_frame_ready(ext_image_copy_capture_frame_v1 *frame);
110 void icc_frame_failed(ext_image_copy_capture_frame_v1 *frame, std::uint32_t reason);
111 void buffer(zwlr_screencopy_frame_v1 *frame, std::uint32_t format, std::uint32_t width, std::uint32_t height, std::uint32_t stride);
112 void linux_dmabuf(zwlr_screencopy_frame_v1 *frame, std::uint32_t format, std::uint32_t width, std::uint32_t height);
113 void buffer_done(zwlr_screencopy_frame_v1 *frame);
114 void flags(zwlr_screencopy_frame_v1 *frame, std::uint32_t flags);
115 void damage(zwlr_screencopy_frame_v1 *frame, std::uint32_t x, std::uint32_t y, std::uint32_t width, std::uint32_t height);
116 void ready(zwlr_screencopy_frame_v1 *frame, std::uint32_t tv_sec_hi, std::uint32_t tv_sec_lo, std::uint32_t tv_nsec);
117 void failed(zwlr_screencopy_frame_v1 *frame);
118
119 frame_t *get_next_frame() {
120 return current_frame == &frames[0] ? &frames[1] : &frames[0];
121 }
122
123 status_e status;
124 std::array<frame_t, 2> frames;
125 frame_t *current_frame;
126 zwlr_screencopy_frame_v1_listener listener;
127
128 private:
129 bool init_gbm();
130 void cleanup_gbm();
131 void create_and_copy_dmabuf(zwlr_screencopy_frame_v1 *frame);
132 void icc_create_and_copy_dmabuf(ext_image_copy_capture_frame_v1 *frame);
133 void icc_begin_constraints();
134 void icc_destroy_session();
135
136 zwp_linux_dmabuf_v1 *dmabuf_interface {nullptr};
137
138 struct {
139 bool supported {false};
140 std::uint32_t format;
141 std::uint32_t width;
142 std::uint32_t height;
143 std::uint32_t stride;
144 } shm_info;
145
146 struct {
147 bool supported {false};
148 std::uint32_t format;
149 std::uint32_t width;
150 std::uint32_t height;
151 } dmabuf_info;
152
153 struct {
154 ext_image_copy_capture_session_v1 *session {nullptr};
155 ext_image_copy_capture_frame_v1 *frame {nullptr};
156 std::uint32_t width {0};
157 std::uint32_t height {0};
158 std::uint32_t format {0};
159 // Keyed by format because the compositor sends one dmabuf_format event
160 // per format it accepts, each with its own modifier list.
161 std::map<std::uint32_t, std::vector<std::uint64_t>> modifiers;
162 bool dmabuf_supported {false};
163 bool cursor {false};
164 bool done {false};
165 } icc_session;
166
167 struct gbm_device *gbm_device {nullptr};
168 // gbm_create_device() borrows the fd, it does not adopt it, so the device
169 // cannot be the only thing holding on to it.
170 int drm_fd {-1};
171 // Render node the compositor renders on, when it told us; empty otherwise.
172 std::string render_node;
173 struct gbm_bo *current_bo {nullptr};
174 struct wl_buffer *current_wl_buffer {nullptr};
175 bool y_invert {false};
176 };
177
178 class monitor_t {
179 public:
180 explicit monitor_t(wl_output *output);
181
182 monitor_t(monitor_t &&) = delete;
183 monitor_t(const monitor_t &) = delete;
184 monitor_t &operator=(const monitor_t &) = delete;
185 monitor_t &operator=(monitor_t &&) = delete;
186
187 void listen(zxdg_output_manager_v1 *output_manager);
188 void xdg_name(zxdg_output_v1 *, const char *name);
189 void xdg_description(zxdg_output_v1 *, const char *description);
190 void xdg_position(zxdg_output_v1 *, std::int32_t x, std::int32_t y);
191 void xdg_size(zxdg_output_v1 *, std::int32_t width, std::int32_t height);
192
193 void xdg_done(zxdg_output_v1 *) {}
194
195 void wl_geometry(wl_output *wl_output, std::int32_t x, std::int32_t y, std::int32_t physical_width, std::int32_t physical_height, std::int32_t subpixel, const char *make, const char *model, std::int32_t transform) {}
196
197 void wl_mode(wl_output *wl_output, std::uint32_t flags, std::int32_t width, std::int32_t height, std::int32_t refresh);
198
199 void wl_done(wl_output *wl_output) {}
200
201 void wl_scale(wl_output *wl_output, std::int32_t factor) {}
202
203 wl_output *output;
204 std::string name;
205 std::string description;
206 platf::touch_port_t viewport;
207 // Size in the compositor's logical coordinates (xdg_output); 0 until known.
208 int logical_width {0};
209 int logical_height {0};
210 wl_output_listener wl_listener;
211 zxdg_output_v1_listener xdg_listener;
212 };
213
214 class interface_t {
215 struct bind_t {
216 std::uint32_t id;
217 std::uint32_t version;
218 };
219
220 public:
229
230 interface_t() noexcept;
231
232 interface_t(interface_t &&) = delete;
233 interface_t(const interface_t &) = delete;
234 interface_t &operator=(const interface_t &) = delete;
235 interface_t &operator=(interface_t &&) = delete;
236
237 void listen(wl_registry *registry);
238
239 bool operator[](interface_e bit) const {
240 return interface[bit];
241 }
242
243 std::vector<std::unique_ptr<monitor_t>> monitors;
244 zwlr_screencopy_manager_v1 *screencopy_manager {nullptr};
245 zwp_linux_dmabuf_v1 *dmabuf_interface {nullptr};
246 zxdg_output_manager_v1 *output_manager {nullptr};
247 ext_image_copy_capture_manager_v1 *image_copy_capture_manager {nullptr};
248 ext_output_image_capture_source_manager_v1 *image_capture_source_manager {nullptr};
249
250 private:
251 void add_interface(wl_registry *registry, std::uint32_t id, const char *interface, std::uint32_t version);
252 void del_interface(wl_registry *registry, uint32_t id);
253
254 std::bitset<MAX_INTERFACES> interface;
255 wl_registry_listener listener;
256 };
257
258 class display_t {
259 public:
266 int init(const char *display_name = nullptr);
267
268 // Roundtrip with Wayland connection
269 void roundtrip();
270
271 // Wait up to the timeout to read and dispatch new events. Returns false on
272 // a timeout and on a dead connection; connection_error() tells the two apart.
273 bool dispatch(std::chrono::milliseconds timeout);
274
281 int connection_error();
282
283 // Get the registry associated with the display
284 // No need to manually free the registry
285 wl_registry *registry();
286
287 inline display_internal_t::pointer get() {
288 return display_internal.get();
289 }
290
291 private:
292 display_internal_t display_internal;
293 bool error_logged {false};
294 };
295
296 std::vector<std::unique_ptr<monitor_t>> monitors(const char *display_name = nullptr);
297
307 std::string compositor_render_node(display_t &display, zwp_linux_dmabuf_v1 *dmabuf_interface);
308
322 void logical_size(int mode_width, int mode_height, int transform, wl_fixed_t scale, int &width, int &height);
323
326 int x, y;
327 int width, height;
328 };
329
338 output_rect_t desktop_envelope(const std::vector<output_rect_t> &outputs);
339
342 int offset_x, offset_y;
343 int env_width, env_height;
344 };
345
358 input_geometry_t input_geometry(const output_rect_t &envelope, const output_rect_t &output, int mode_width, int mode_height);
359
365 bool configure_virtual_output(const std::string &output_name, int width, int height, int refresh_rate, bool exclusive);
370
382 bool render_node_can_allocate(const char *node);
383
404
412
427
428 int init();
429} // namespace wl
430#else
431
432struct wl_output;
433struct zxdg_output_manager_v1;
434
435namespace wl {
436 class monitor_t {
437 public:
438 monitor_t(wl_output *output);
439
440 monitor_t(monitor_t &&) = delete;
441 monitor_t(const monitor_t &) = delete;
442 monitor_t &operator=(const monitor_t &) = delete;
443 monitor_t &operator=(monitor_t &&) = delete;
444
445 void listen(zxdg_output_manager_v1 *output_manager);
446
447 wl_output *output;
448 std::string name;
449 std::string description;
450 platf::touch_port_t viewport;
451 };
452
453 inline std::vector<std::unique_ptr<monitor_t>> monitors(const char *display_name = nullptr) {
454 return {};
455 }
456
457 inline int init() {
458 return -1;
459 }
460
461 inline bool configure_virtual_output(const std::string &, int, int, int, bool) {
462 return false;
463 }
464
465 inline bool restore_virtual_output_layout() {
466 return false;
467 }
468
469 inline bool render_node_can_allocate(const char *) {
470 // No GBM linked in without Wayland, so there is nothing to ask. Answering
471 // yes leaves every caller on the behaviour it had before the probe existed.
472 return true;
473 }
474
475 inline bool output_management_supported() {
476 return false;
477 }
478
479 struct session_protocols_t {
480 bool connected;
482 bool screencopy;
485 bool linux_dmabuf;
486 bool xdg_output;
487 };
488
490 return {};
491 }
492} // namespace wl
493#endif
int init(const char *display_name=nullptr)
Initialize display. If display_name == nullptr -> display_name = std::getenv("WAYLAND_DISPLAY").
Definition wayland.cpp:112
int connection_error()
The errno-style code of the connection's fatal error, 0 for none. A protocol error (EPROTO) is fatal ...
Definition wayland.cpp:176
bool dispatch(std::chrono::milliseconds timeout)
Waits up to the specified timeout to dispatch new events on the wl_display.
Definition wayland.cpp:143
bool icc_ready() const
Whether an ICC session is open and has finished negotiating.
Definition wayland.h:89
void icc_capture(bool blend_cursor=false)
Request one frame from an established ICC session.
Definition wayland.cpp:1347
bool icc_active() const
Whether this object is driving ICC rather than screencopy.
Definition wayland.h:94
bool icc_listen(ext_image_copy_capture_manager_v1 *manager, ext_output_image_capture_source_manager_v1 *source_manager, zwp_linux_dmabuf_v1 *dmabuf_interface, wl_output *output, bool blend_cursor=false)
Open an ext-image-copy-capture session on an output.
Definition wayland.cpp:1303
@ WAITING
Waiting for a frame.
Definition wayland.h:52
@ READY
Frame is ready.
Definition wayland.h:53
@ REINIT
Reinitialize the frame.
Definition wayland.h:54
void set_render_node(std::string node)
Name the render node the compositor renders on, so init_gbm() allocates there. See compositor_render_...
Definition wayland.cpp:856
@ IMAGE_COPY_CAPTURE
ext-image-copy-capture manager
Definition wayland.h:225
@ IMAGE_CAPTURE_SOURCE
ext-image-capture-source manager, names an output as a source
Definition wayland.h:226
@ WLR_EXPORT_DMABUF
screencopy manager
Definition wayland.h:223
@ LINUX_DMABUF
linux-dmabuf protocol
Definition wayland.h:224
@ MAX_INTERFACES
Maximum number of interfaces.
Definition wayland.h:227
@ XDG_OUTPUT
xdg-output
Definition wayland.h:222
Declarations for graphics related functions.
Definition wayland.cpp:53
bool dmabuf_capture_exhausted()
Whether the dmabuf capture path has failed too often to be retried.
Definition wayland.cpp:708
bool restore_virtual_output_layout()
Definition wayland.cpp:1824
output_rect_t desktop_envelope(const std::vector< output_rect_t > &outputs)
The desktop envelope: the bounding box of every output in logical coordinates, origin included....
Definition wayland.cpp:67
void logical_size(int mode_width, int mode_height, int transform, wl_fixed_t scale, int &width, int &height)
Size of a head in the compositor's logical coordinates, the wlr counterpart of mutter::state_t::logic...
Definition wayland.cpp:54
bool configure_virtual_output(const std::string &output_name, int width, int height, int refresh_rate, bool exclusive)
Definition wayland.cpp:1760
input_geometry_t input_geometry(const output_rect_t &envelope, const output_rect_t &output, int mode_width, int mode_height)
Express an output's offset within the envelope, and the envelope, in that output's own capture pixels...
Definition wayland.cpp:87
std::string compositor_render_node(display_t &display, zwp_linux_dmabuf_v1 *dmabuf_interface)
Ask the compositor which GPU it renders on.
Definition wayland.cpp:820
bool render_node_can_allocate(const char *node)
Whether GBM can actually allocate a buffer on this render node.
Definition wayland.cpp:1745
bool output_management_supported()
Definition wayland.cpp:1846
session_protocols_t probe_protocols()
Probe the session's globals in a single registry roundtrip.
Definition wayland.cpp:1851
Which Wayland globals the running session advertises.
Definition wayland.h:395
bool screencopy
zwlr_screencopy_manager_v1: the wlgrab capture backend.
Definition wayland.h:398
bool linux_dmabuf
zwp_linux_dmabuf_v1: without it capture is a CPU copy.
Definition wayland.h:401
bool output_management
zwlr_output_manager_v1: enable/mode/position an output.
Definition wayland.h:397
bool xdg_output
zxdg_output_manager_v1: logical geometry of each output.
Definition wayland.h:402
bool connected
A Wayland display was reachable at all.
Definition wayland.h:396
bool image_copy_capture
ext_image_copy_capture_manager_v1: the successor to screencopy.
Definition wayland.h:399
bool output_capture_source
ext_output_image_capture_source_manager_v1: names an output as a source.
Definition wayland.h:400