Debian Codegraph @

gst-libs/gst/wayland/gstwldisplay.c

raw
1 /* GStreamer Wayland Library
2 *
3 * Copyright (C) 2014 Collabora Ltd.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include "gstwldisplay.h"
26 #include "gstwloutput-private.h"
27
28 #include "color-management-v1-client-protocol.h"
29 #include "color-representation-v1-client-protocol.h"
30 #include "fullscreen-shell-unstable-v1-client-protocol.h"
31 #include "linux-dmabuf-unstable-v1-client-protocol.h"
32 #include "single-pixel-buffer-v1-client-protocol.h"
33 #include "viewporter-client-protocol.h"
34 #include "xdg-shell-client-protocol.h"
35
36 #include <errno.h>
37 #include <drm_fourcc.h>
38
39 #define GST_CAT_DEFAULT gst_wl_display_debug
40 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
41
42 typedef struct _GstWlDisplayPrivate
43 {
44 /* public objects */
45 struct wl_display *display;
46 struct wl_display *display_wrapper;
47 struct wl_event_queue *queue;
48
49 /* globals */
50 struct wl_registry *registry;
51 struct wl_compositor *compositor;
52 struct wl_subcompositor *subcompositor;
53 struct xdg_wm_base *xdg_wm_base;
54 struct zwp_fullscreen_shell_v1 *fullscreen_shell;
55 struct wp_single_pixel_buffer_manager_v1 *single_pixel_buffer;
56 struct wl_shm *shm;
57 struct wp_viewporter *viewporter;
58 struct zwp_linux_dmabuf_v1 *dmabuf;
59 struct wp_color_manager_v1 *color;
60 struct wp_color_representation_manager_v1 *color_representation;
61
62 GArray *shm_formats;
63 GArray *dmabuf_formats;
64 GArray *dmabuf_modifiers;
65
66 gboolean color_parametric_creator_supported;
67 gboolean color_mastering_display_supported;
68 GArray *color_transfer_functions;
69 GArray *color_primaries;
70 GArray *color_alpha_modes;
71 GArray *color_coefficients;
72 GArray *color_coefficients_range;
73
74 GMutex outputs_mutex;
75 GHashTable *outputs;
76
77 /* private */
78 gboolean own_display;
79 GThread *thread;
80 GstPoll *wl_fd_poll;
81
82 GRecMutex sync_mutex;
83
84 GMutex buffers_mutex;
85 GHashTable *buffers;
86 gboolean shutting_down;
87 } GstWlDisplayPrivate;
88
89 G_DEFINE_TYPE_WITH_CODE (GstWlDisplay, gst_wl_display, G_TYPE_OBJECT,
90 G_ADD_PRIVATE (GstWlDisplay)
91 GST_DEBUG_CATEGORY_INIT (gst_wl_display_debug,
92 "wldisplay", 0, "wldisplay library");
93 );
94
95 static void gst_wl_display_finalize (GObject * gobject);
96
97 static void
98 gst_wl_display_class_init (GstWlDisplayClass * klass)
99 {
100 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
101 gobject_class->finalize = gst_wl_display_finalize;
102 }
103
104 static void
105 gst_wl_display_init (GstWlDisplay * self)
106 {
107 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
108
109 priv->shm_formats = g_array_new (FALSE, FALSE, sizeof (uint32_t));
110 priv->dmabuf_formats = g_array_new (FALSE, FALSE, sizeof (uint32_t));
111 priv->dmabuf_modifiers = g_array_new (FALSE, FALSE, sizeof (guint64));
112 priv->color_transfer_functions =
113 g_array_new (FALSE, FALSE, sizeof (uint32_t));
114 priv->color_primaries = g_array_new (FALSE, FALSE, sizeof (uint32_t));
115 priv->color_coefficients = g_array_new (FALSE, FALSE, sizeof (uint32_t));
116 priv->color_coefficients_range =
117 g_array_new (FALSE, FALSE, sizeof (uint32_t));
118 priv->color_alpha_modes = g_array_new (FALSE, FALSE, sizeof (uint32_t));
119 priv->wl_fd_poll = gst_poll_new (TRUE);
120 priv->buffers = g_hash_table_new (g_direct_hash, g_direct_equal);
121 g_mutex_init (&priv->buffers_mutex);
122 g_rec_mutex_init (&priv->sync_mutex);
123
124 g_mutex_init (&priv->outputs_mutex);
125 priv->outputs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
126 (GDestroyNotify) g_object_unref);
127
128 gst_wl_linux_dmabuf_init_once ();
129 gst_wl_shm_init_once ();
130 gst_shm_allocator_init_once ();
131 gst_wl_videoformat_init_once ();
132 }
133
134 static void
135 gst_wl_ref_wl_buffer (gpointer key, gpointer value, gpointer user_data)
136 {
137 g_object_ref (value);
138 }
139
140 static void
141 gst_wl_display_finalize (GObject * gobject)
142 {
143 GstWlDisplay *self = GST_WL_DISPLAY (gobject);
144 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
145
146 gst_poll_set_flushing (priv->wl_fd_poll, TRUE);
147 if (priv->thread)
148 g_thread_join (priv->thread);
149
150 /* to avoid buffers being unregistered from another thread
151 * at the same time, take their ownership */
152 g_mutex_lock (&priv->buffers_mutex);
153 priv->shutting_down = TRUE;
154 g_hash_table_foreach (priv->buffers, gst_wl_ref_wl_buffer, NULL);
155 g_mutex_unlock (&priv->buffers_mutex);
156
157 g_hash_table_foreach (priv->buffers,
158 (GHFunc) gst_wl_buffer_force_release_and_unref, NULL);
159 g_hash_table_remove_all (priv->buffers);
160
161 g_array_unref (priv->shm_formats);
162 g_array_unref (priv->dmabuf_formats);
163 g_array_unref (priv->dmabuf_modifiers);
164
165 g_array_unref (priv->color_transfer_functions);
166 g_array_unref (priv->color_primaries);
167 g_array_unref (priv->color_alpha_modes);
168 g_array_unref (priv->color_coefficients);
169 g_array_unref (priv->color_coefficients_range);
170
171 gst_poll_free (priv->wl_fd_poll);
172 g_hash_table_unref (priv->buffers);
173 g_mutex_clear (&priv->buffers_mutex);
174 g_rec_mutex_clear (&priv->sync_mutex);
175
176 g_mutex_clear (&priv->outputs_mutex);
177 g_hash_table_unref (priv->outputs);
178
179 if (priv->color)
180 wp_color_manager_v1_destroy (priv->color);
181
182 if (priv->color_representation)
183 wp_color_representation_manager_v1_destroy (priv->color_representation);
184
185 if (priv->viewporter)
186 wp_viewporter_destroy (priv->viewporter);
187
188 if (priv->shm)
189 wl_shm_destroy (priv->shm);
190
191 if (priv->dmabuf)
192 zwp_linux_dmabuf_v1_destroy (priv->dmabuf);
193
194 if (priv->xdg_wm_base)
195 xdg_wm_base_destroy (priv->xdg_wm_base);
196
197 if (priv->fullscreen_shell)
198 zwp_fullscreen_shell_v1_release (priv->fullscreen_shell);
199
200 if (priv->single_pixel_buffer)
201 wp_single_pixel_buffer_manager_v1_destroy (priv->single_pixel_buffer);
202
203 if (priv->compositor)
204 wl_compositor_destroy (priv->compositor);
205
206 if (priv->subcompositor)
207 wl_subcompositor_destroy (priv->subcompositor);
208
209 if (priv->registry)
210 wl_registry_destroy (priv->registry);
211
212 if (priv->display_wrapper)
213 wl_proxy_wrapper_destroy (priv->display_wrapper);
214
215 if (priv->queue)
216 wl_event_queue_destroy (priv->queue);
217
218 if (priv->own_display) {
219 wl_display_flush (priv->display);
220 wl_display_disconnect (priv->display);
221 }
222
223 G_OBJECT_CLASS (gst_wl_display_parent_class)->finalize (gobject);
224 }
225
226 static void
227 shm_format (void *data, struct wl_shm *wl_shm, uint32_t format)
228 {
229 GstWlDisplay *self = data;
230 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
231
232 g_array_append_val (priv->shm_formats, format);
233 }
234
235 static const struct wl_shm_listener shm_listener = {
236 shm_format
237 };
238
239 static void
240 dmabuf_format (void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf,
241 uint32_t format)
242 {
243 }
244
245 static void
246 dmabuf_modifier (void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf,
247 uint32_t format, uint32_t modifier_hi, uint32_t modifier_lo)
248 {
249 GstWlDisplay *self = data;
250 guint64 modifier = (guint64) modifier_hi << 32 | modifier_lo;
251 GstVideoFormat gst_format = gst_wl_dmabuf_format_to_video_format (format);
252 static uint32_t last_format = 0;
253
254 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
255
256 /*
257 * Ignore unsupported formats along with implicit modifiers. Implicit
258 * modifiers have been source of garbled output for many many years and it
259 * was decided that we prefer disabling zero-copy over risking a bad output.
260 */
261 if (format == DRM_FORMAT_INVALID || modifier == DRM_FORMAT_MOD_INVALID)
262 return;
263
264 if (last_format == 0) {
265 GST_INFO ("===== All DMA Formats With Modifiers =====");
266 GST_INFO ("| Gst Format | DRM Format |");
267 }
268
269 if (last_format != format) {
270 GST_INFO ("|-----------------------------------------");
271 last_format = format;
272 }
273
274 GST_INFO ("| %-12s | %-23s |",
275 (modifier == 0) ? gst_video_format_to_string (gst_format) : "",
276 gst_video_dma_drm_fourcc_to_string (format, modifier));
277
278 g_array_append_val (priv->dmabuf_formats, format);
279 g_array_append_val (priv->dmabuf_modifiers, modifier);
280 }
281
282 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
283 dmabuf_format,
284 dmabuf_modifier,
285 };
286
287 static void
288 color_supported_intent (void *data,
289 struct wp_color_manager_v1 *wp_color_manager_v1, uint32_t render_intent)
290 {
291 }
292
293 static void
294 color_supported_feature (void *data,
295 struct wp_color_manager_v1 *wp_color_manager_v1, uint32_t feature)
296 {
297 GstWlDisplay *self = data;
298 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
299
300 switch (feature) {
301 case WP_COLOR_MANAGER_V1_FEATURE_PARAMETRIC:
302 GST_INFO_OBJECT (self, "New_parametric_creator supported");
303 priv->color_parametric_creator_supported = TRUE;
304 break;
305 case WP_COLOR_MANAGER_V1_FEATURE_SET_MASTERING_DISPLAY_PRIMARIES:
306 GST_INFO_OBJECT (self, "Mastering Display supported");
307 priv->color_mastering_display_supported = TRUE;
308 break;
309 default:
310 break;
311 }
312 }
313
314 static void
315 color_supported_tf_named (void *data,
316 struct wp_color_manager_v1 *wp_color_manager_v1, uint32_t tf)
317 {
318 GstWlDisplay *self = data;
319 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
320
321 GST_INFO_OBJECT (self, "Supported transfer function 0x%x", tf);
322 g_array_append_val (priv->color_transfer_functions, tf);
323 }
324
325 static void
326 color_supported_primaries_named (void *data,
327 struct wp_color_manager_v1 *wp_color_manager_v1, uint32_t primaries)
328 {
329 GstWlDisplay *self = data;
330 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
331
332 GST_INFO_OBJECT (self, "Supported primaries: 0x%x", primaries);
333 g_array_append_val (priv->color_primaries, primaries);
334 }
335
336 static void
337 color_done (void *data, struct wp_color_manager_v1 *wp_color_manager_v1)
338 {
339 }
340
341 static const struct wp_color_manager_v1_listener color_listener = {
342 .supported_intent = color_supported_intent,
343 .supported_feature = color_supported_feature,
344 .supported_tf_named = color_supported_tf_named,
345 .supported_primaries_named = color_supported_primaries_named,
346 .done = color_done,
347 };
348
349 static void
350 color_representation_supported_alpha_mode (void *data,
351 struct wp_color_representation_manager_v1
352 *wp_color_representation_manager_v1, uint32_t alpha_mode)
353 {
354 GstWlDisplay *self = data;
355 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
356
357 GST_INFO_OBJECT (self, "Supported alpha mode: 0x%x", alpha_mode);
358 g_array_append_val (priv->color_alpha_modes, alpha_mode);
359 }
360
361 static void
362 color_representation_supported_coefficients_and_ranges (void *data,
363 struct wp_color_representation_manager_v1
364 *wp_color_representation_manager_v1, uint32_t coefficients, uint32_t range)
365 {
366 GstWlDisplay *self = data;
367 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
368
369 GST_INFO_OBJECT (self, "Supported coefficients and range: 0x%x/0x%x",
370 coefficients, range);
371 g_array_append_val (priv->color_coefficients, coefficients);
372 g_array_append_val (priv->color_coefficients_range, range);
373 }
374
375 static void
376 color_representation_done (void *data, struct wp_color_representation_manager_v1
377 *wp_color_representation_manager_v1)
378 {
379 }
380
381 static const struct wp_color_representation_manager_v1_listener
382 color_representation_listener = {
383 .supported_alpha_mode = color_representation_supported_alpha_mode,
384 .supported_coefficients_and_ranges =
385 color_representation_supported_coefficients_and_ranges,
386 .done = color_representation_done,
387 };
388
389 static void
390 output_geometry (void *data, struct wl_output *wl_output,
391 int32_t x, int32_t y, int32_t physical_width, int32_t physical_height,
392 int32_t subpixel, const char *make, const char *model, int32_t transform)
393 {
394 GstWlOutput *output = GST_WL_OUTPUT (data);
395 gst_wl_output_set_geometry (output, x, y, physical_width, physical_height,
396 subpixel, make, model, transform);
397 }
398
399 static void
400 output_mode (void *data, struct wl_output *wl_output,
401 uint32_t flags, int32_t width, int32_t height, int32_t refresh)
402 {
403 GstWlOutput *output = GST_WL_OUTPUT (data);
404 gst_wl_output_set_mode (output, flags, width, height, refresh);
405 }
406
407 static void
408 output_scale (void *data, struct wl_output *wl_output, int32_t factor)
409 {
410 GstWlOutput *output = GST_WL_OUTPUT (data);
411 gst_wl_output_set_scale (output, factor);
412 }
413
414 static void
415 output_name (void *data, struct wl_output *wl_output, const char *name)
416 {
417 GstWlOutput *output = GST_WL_OUTPUT (data);
418 gst_wl_output_set_name (output, name);
419 }
420
421 static void
422 output_description (void *data, struct wl_output *wl_output,
423 const char *description)
424 {
425 GstWlOutput *output = GST_WL_OUTPUT (data);
426 gst_wl_output_set_description (output, description);
427 }
428
429 static void
430 output_done (void *data, struct wl_output *wl_output)
431 {
432 GstWlOutput *output = GST_WL_OUTPUT (data);
433 GstWlDisplay *self = g_object_steal_data (G_OBJECT (output), "display");
434 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
435 const gchar *name = gst_wl_output_get_name (output);
436
437 GST_INFO ("Adding output %s (%p):", name, wl_output);
438 GST_INFO (" Make: %s", gst_wl_output_get_make (output));
439 GST_INFO (" Model: %s", gst_wl_output_get_model (output));
440
441 #define ARGS(r) (r) /1000 , (r) % 1000
442 GST_INFO (" Mode: %ix%i px %i.%ifps flags %x",
443 gst_wl_output_get_width (output), gst_wl_output_get_height (output),
444 ARGS (gst_wl_output_get_refresh (output)),
445 gst_wl_output_get_mode_flags (output));
446 #undef ARGS
447
448 GST_INFO (" Geometry: %i,%i %ix%i mm scale %i",
449 gst_wl_output_get_x (output), gst_wl_output_get_y (output),
450 gst_wl_output_get_physical_width (output),
451 gst_wl_output_get_physical_height (output),
452 gst_wl_output_get_scale (output));
453 GST_INFO (" Subpixel %i", gst_wl_output_get_subpixel (output));
454 GST_INFO (" Transform: %i", gst_wl_output_get_transform (output));
455 GST_INFO ("---");
456
457 g_mutex_lock (&priv->outputs_mutex);
458 if (name)
459 g_hash_table_replace (priv->outputs, g_strdup (name), output);
460 else
461 GST_WARNING ("Compositor has provided an unnamed output, ignoring.");
462 g_mutex_unlock (&priv->outputs_mutex);
463 }
464
465 static const struct wl_output_listener output_listener = {
466 output_geometry,
467 output_mode,
468 output_done,
469 output_scale,
470 output_name,
471 output_description,
472 };
473
474 gboolean
475 gst_wl_display_check_format_for_shm (GstWlDisplay * self,
476 const GstVideoInfo * video_info)
477 {
478 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
479 GstVideoFormat format = GST_VIDEO_INFO_FORMAT (video_info);
480 enum wl_shm_format shm_fmt;
481 GArray *formats;
482 guint i;
483
484 shm_fmt = gst_video_format_to_wl_shm_format (format);
485 if (shm_fmt == (enum wl_shm_format) -1)
486 return FALSE;
487
488 formats = priv->shm_formats;
489 for (i = 0; i < formats->len; i++) {
490 if (g_array_index (formats, uint32_t, i) == shm_fmt)
491 return TRUE;
492 }
493
494 return FALSE;
495 }
496
497 gboolean
498 gst_wl_display_check_format_for_dmabuf (GstWlDisplay * self,
499 const GstVideoInfoDmaDrm * drm_info)
500 {
501 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
502 guint64 modifier = drm_info->drm_modifier;
503 guint fourcc = drm_info->drm_fourcc;
504 GArray *formats, *modifiers;
505 guint i;
506
507 if (!priv->dmabuf)
508 return FALSE;
509
510 formats = priv->dmabuf_formats;
511 modifiers = priv->dmabuf_modifiers;
512 for (i = 0; i < formats->len; i++) {
513 if (g_array_index (formats, uint32_t, i) == fourcc) {
514 if (g_array_index (modifiers, guint64, i) == modifier) {
515 return TRUE;
516 }
517 }
518 }
519
520 return FALSE;
521 }
522
523 static void
524 handle_xdg_wm_base_ping (void *user_data, struct xdg_wm_base *xdg_wm_base,
525 uint32_t serial)
526 {
527 xdg_wm_base_pong (xdg_wm_base, serial);
528 }
529
530 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
531 handle_xdg_wm_base_ping
532 };
533
534 static void
535 registry_handle_global (void *data, struct wl_registry *registry,
536 uint32_t id, const char *interface, uint32_t version)
537 {
538 GstWlDisplay *self = data;
539 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
540
541 if (g_strcmp0 (interface, "wl_compositor") == 0) {
542 priv->compositor = wl_registry_bind (registry, id, &wl_compositor_interface,
543 MIN (version, 4));
544 } else if (g_strcmp0 (interface, "wl_subcompositor") == 0) {
545 priv->subcompositor =
546 wl_registry_bind (registry, id, &wl_subcompositor_interface, 1);
547 } else if (g_strcmp0 (interface, "xdg_wm_base") == 0) {
548 priv->xdg_wm_base =
549 wl_registry_bind (registry, id, &xdg_wm_base_interface, 1);
550 xdg_wm_base_add_listener (priv->xdg_wm_base, &xdg_wm_base_listener, self);
551 } else if (g_strcmp0 (interface, "zwp_fullscreen_shell_v1") == 0) {
552 priv->fullscreen_shell = wl_registry_bind (registry, id,
553 &zwp_fullscreen_shell_v1_interface, 1);
554 } else if (g_strcmp0 (interface, "wl_shm") == 0) {
555 priv->shm = wl_registry_bind (registry, id, &wl_shm_interface, 1);
556 wl_shm_add_listener (priv->shm, &shm_listener, self);
557 } else if (g_strcmp0 (interface, "wp_viewporter") == 0) {
558 priv->viewporter =
559 wl_registry_bind (registry, id, &wp_viewporter_interface, 1);
560 } else if (g_strcmp0 (interface, "zwp_linux_dmabuf_v1") == 0) {
561 priv->dmabuf =
562 wl_registry_bind (registry, id, &zwp_linux_dmabuf_v1_interface, 3);
563 zwp_linux_dmabuf_v1_add_listener (priv->dmabuf, &dmabuf_listener, self);
564 } else if (g_strcmp0 (interface, "wp_single_pixel_buffer_manager_v1") == 0) {
565 priv->single_pixel_buffer =
566 wl_registry_bind (registry, id,
567 &wp_single_pixel_buffer_manager_v1_interface, 1);
568 } else if (g_strcmp0 (interface, wp_color_manager_v1_interface.name) == 0) {
569 priv->color = wl_registry_bind (registry, id,
570 &wp_color_manager_v1_interface, 1);
571 wp_color_manager_v1_add_listener (priv->color, &color_listener, self);
572 } else if (g_strcmp0 (interface,
573 wp_color_representation_manager_v1_interface.name) == 0) {
574 priv->color_representation =
575 wl_registry_bind (registry, id,
576 &wp_color_representation_manager_v1_interface, 1);
577 wp_color_representation_manager_v1_add_listener (priv->color_representation,
578 &color_representation_listener, self);
579 } else if (g_strcmp0 (interface, "wl_output") == 0) {
580 struct wl_output *wl_output =
581 wl_registry_bind (registry, id, &wl_output_interface, MIN (version, 4));
582 GstWlOutput *output = gst_wl_output_new (wl_output, id);
583 g_object_set_data (G_OBJECT (output), "display", self);
584 wl_output_add_listener (wl_output, &output_listener, output);
585 }
586 }
587
588 static void
589 registry_handle_global_remove (void *data, struct wl_registry *registry,
590 uint32_t name)
591 {
592 GstWlDisplay *self = data;
593 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
594
595 g_mutex_lock (&priv->outputs_mutex);
596
597 GHashTableIter iter;
598 gpointer key, value;
599 g_hash_table_iter_init (&iter, priv->outputs);
600 while (g_hash_table_iter_next (&iter, &key, &value)) {
601 GstWlOutput *output = value;
602
603 if (gst_wl_output_get_id (output) == name) {
604 g_hash_table_iter_remove (&iter);
605 break;
606 }
607 }
608
609 g_mutex_unlock (&priv->outputs_mutex);
610 }
611
612 static const struct wl_registry_listener registry_listener = {
613 registry_handle_global,
614 registry_handle_global_remove
615 };
616
617 static gpointer
618 gst_wl_display_thread_run (gpointer data)
619 {
620 GstWlDisplay *self = data;
621 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
622 GstPollFD pollfd = GST_POLL_FD_INIT;
623
624 pollfd.fd = wl_display_get_fd (priv->display);
625 gst_poll_add_fd (priv->wl_fd_poll, &pollfd);
626 gst_poll_fd_ctl_read (priv->wl_fd_poll, &pollfd, TRUE);
627
628 /* main loop */
629 while (1) {
630 g_rec_mutex_lock (&priv->sync_mutex);
631 while (wl_display_prepare_read_queue (priv->display, priv->queue) != 0) {
632 if (wl_display_dispatch_queue_pending (priv->display, priv->queue) == -1) {
633 g_rec_mutex_unlock (&priv->sync_mutex);
634 goto error;
635 }
636 }
637 g_rec_mutex_unlock (&priv->sync_mutex);
638 wl_display_flush (priv->display);
639
640 if (gst_poll_wait (priv->wl_fd_poll, GST_CLOCK_TIME_NONE) < 0) {
641 gboolean normal = (errno == EBUSY);
642 wl_display_cancel_read (priv->display);
643 if (normal)
644 break;
645 else
646 goto error;
647 }
648 if (wl_display_read_events (priv->display) == -1)
649 goto error;
650
651 g_rec_mutex_lock (&priv->sync_mutex);
652 if (wl_display_dispatch_queue_pending (priv->display, priv->queue) == -1) {
653 g_rec_mutex_unlock (&priv->sync_mutex);
654 goto error;
655 }
656 g_rec_mutex_unlock (&priv->sync_mutex);
657 }
658
659 return NULL;
660
661 error:
662 GST_ERROR ("Error communicating with the wayland server");
663 return NULL;
664 }
665
666 GstWlDisplay *
667 gst_wl_display_new (const gchar * name, GError ** error)
668 {
669 struct wl_display *display;
670
671 display = wl_display_connect (name);
672
673 if (!display) {
674 *error = g_error_new (g_quark_from_static_string ("GstWlDisplay"), 0,
675 "Failed to connect to the wayland display '%s'",
676 name ? name : "(default)");
677 return NULL;
678 } else {
679 return gst_wl_display_new_existing (display, TRUE, error);
680 }
681 }
682
683 GstWlDisplay *
684 gst_wl_display_new_existing (struct wl_display *display,
685 gboolean take_ownership, GError ** error)
686 {
687 GstWlDisplay *self;
688 GstWlDisplayPrivate *priv;
689 GError *err = NULL;
690 gint i;
691
692 g_return_val_if_fail (display != NULL, NULL);
693
694 self = g_object_new (GST_TYPE_WL_DISPLAY, NULL);
695 priv = gst_wl_display_get_instance_private (self);
696 priv->display = display;
697 priv->display_wrapper = wl_proxy_create_wrapper (display);
698 priv->own_display = take_ownership;
699
700 #ifdef HAVE_WL_EVENT_QUEUE_NAME
701 priv->queue = wl_display_create_queue_with_name (priv->display,
702 "GStreamer display queue");
703 #else
704 priv->queue = wl_display_create_queue (priv->display);
705 #endif
706 wl_proxy_set_queue ((struct wl_proxy *) priv->display_wrapper, priv->queue);
707 priv->registry = wl_display_get_registry (priv->display_wrapper);
708 wl_registry_add_listener (priv->registry, &registry_listener, self);
709
710 /* we need exactly 2 roundtrips to discover global objects and their state */
711 for (i = 0; i < 2; i++) {
712 if (wl_display_roundtrip_queue (priv->display, priv->queue) < 0) {
713 *error = g_error_new (g_quark_from_static_string ("GstWlDisplay"), 0,
714 "Error communicating with the wayland display");
715 g_object_unref (self);
716 return NULL;
717 }
718 }
719
720 /* verify we got all the required interfaces */
721 #define VERIFY_INTERFACE_EXISTS(var, interface) \
722 if (!priv->var) { \
723 g_set_error (error, g_quark_from_static_string ("GstWlDisplay"), 0, \
724 "Could not bind to " interface ". Either it is not implemented in " \
725 "the compositor, or the implemented version doesn't match"); \
726 g_object_unref (self); \
727 return NULL; \
728 }
729
730 VERIFY_INTERFACE_EXISTS (compositor, "wl_compositor");
731 VERIFY_INTERFACE_EXISTS (subcompositor, "wl_subcompositor");
732 VERIFY_INTERFACE_EXISTS (shm, "wl_shm");
733
734 #undef VERIFY_INTERFACE_EXISTS
735
736 /* We make the viewporter optional even though it may cause bad display.
737 * This is so one can test wayland display on older compositor or on
738 * compositor that don't implement this extension. */
739 if (!priv->viewporter) {
740 g_warning ("Wayland compositor is missing the ability to scale, video "
741 "display may not work properly.");
742 }
743
744 if (!priv->dmabuf) {
745 g_warning ("Could not bind to zwp_linux_dmabuf_v1");
746 }
747
748 if (!priv->xdg_wm_base && !priv->fullscreen_shell) {
749 /* If wl_surface and wl_display are passed via GstContext
750 * xdg_shell and zwp_fullscreen_shell are not used.
751 * In this case is correct to continue.
752 */
753 g_warning ("Could not bind to either xdg_wm_base or zwp_fullscreen_shell, "
754 "video display may not work properly.");
755 }
756
757 priv->thread = g_thread_try_new ("GstWlDisplay", gst_wl_display_thread_run,
758 self, &err);
759 if (err) {
760 g_propagate_prefixed_error (error, err,
761 "Failed to start thread for the display's events");
762 g_object_unref (self);
763 return NULL;
764 }
765
766 return self;
767 }
768
769 void
770 gst_wl_display_register_buffer (GstWlDisplay * self, gpointer gstmem,
771 gpointer wlbuffer)
772 {
773 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
774
775 g_assert (!priv->shutting_down);
776
777 GST_TRACE_OBJECT (self, "registering GstWlBuffer %p to GstMem %p",
778 wlbuffer, gstmem);
779
780 g_mutex_lock (&priv->buffers_mutex);
781 g_hash_table_replace (priv->buffers, gstmem, wlbuffer);
782 g_mutex_unlock (&priv->buffers_mutex);
783 }
784
785 gpointer
786 gst_wl_display_lookup_buffer (GstWlDisplay * self, gpointer gstmem)
787 {
788 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
789 gpointer wlbuffer;
790
791 g_mutex_lock (&priv->buffers_mutex);
792 wlbuffer = g_hash_table_lookup (priv->buffers, gstmem);
793 g_mutex_unlock (&priv->buffers_mutex);
794 return wlbuffer;
795 }
796
797 void
798 gst_wl_display_unregister_buffer (GstWlDisplay * self, gpointer gstmem)
799 {
800 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
801
802 GST_TRACE_OBJECT (self, "unregistering GstWlBuffer owned by %p", gstmem);
803
804 g_mutex_lock (&priv->buffers_mutex);
805 if (G_LIKELY (!priv->shutting_down))
806 g_hash_table_remove (priv->buffers, gstmem);
807 g_mutex_unlock (&priv->buffers_mutex);
808 }
809
810 /* gst_wl_display_sync
811 *
812 * A syncronized version of `wl_display_sink` that ensures that the
813 * callback will not be dispatched before the listener has been attached.
814 */
815 struct wl_callback *
816 gst_wl_display_sync (GstWlDisplay * self,
817 const struct wl_callback_listener *listener, gpointer data)
818 {
819 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
820 struct wl_callback *callback;
821
822 g_rec_mutex_lock (&priv->sync_mutex);
823
824 callback = wl_display_sync (priv->display_wrapper);
825 if (callback && listener)
826 wl_callback_add_listener (callback, listener, data);
827
828 g_rec_mutex_unlock (&priv->sync_mutex);
829
830 return callback;
831 }
832
833 /* gst_wl_display_object_destroy
834 *
835 * A syncronized version of `xxx_destroy` that ensures that the
836 * once this function returns, the destroy_func will either have already completed,
837 * or will never be called.
838 */
839 void
840 gst_wl_display_object_destroy (GstWlDisplay * self,
841 gpointer * object, GDestroyNotify destroy_func)
842 {
843 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
844
845 g_rec_mutex_lock (&priv->sync_mutex);
846
847 if (*object) {
848 destroy_func (*object);
849 *object = NULL;
850 }
851
852 g_rec_mutex_unlock (&priv->sync_mutex);
853 }
854
855 /* gst_wl_display_callback_destroy
856 *
857 * A syncronized version of `wl_callback_destroy` that ensures that the
858 * once this function returns, the callback will either have already completed,
859 * or will never be called.
860 */
861 void
862 gst_wl_display_callback_destroy (GstWlDisplay * self,
863 struct wl_callback **callback)
864 {
865 gst_wl_display_object_destroy (self, (gpointer *) callback,
866 (GDestroyNotify) wl_callback_destroy);
867 }
868
869 struct wl_display *
870 gst_wl_display_get_display (GstWlDisplay * self)
871 {
872 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
873
874 return priv->display;
875 }
876
877 struct wl_event_queue *
878 gst_wl_display_get_event_queue (GstWlDisplay * self)
879 {
880 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
881
882 return priv->queue;
883 }
884
885 struct wl_compositor *
886 gst_wl_display_get_compositor (GstWlDisplay * self)
887 {
888 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
889
890 return priv->compositor;
891 }
892
893 struct wl_subcompositor *
894 gst_wl_display_get_subcompositor (GstWlDisplay * self)
895 {
896 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
897
898 return priv->subcompositor;
899 }
900
901 struct xdg_wm_base *
902 gst_wl_display_get_xdg_wm_base (GstWlDisplay * self)
903 {
904 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
905
906 return priv->xdg_wm_base;
907 }
908
909 struct zwp_fullscreen_shell_v1 *
910 gst_wl_display_get_fullscreen_shell_v1 (GstWlDisplay * self)
911 {
912 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
913
914 return priv->fullscreen_shell;
915 }
916
917 struct wp_viewporter *
918 gst_wl_display_get_viewporter (GstWlDisplay * self)
919 {
920 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
921
922 return priv->viewporter;
923 }
924
925 struct wl_shm *
926 gst_wl_display_get_shm (GstWlDisplay * self)
927 {
928 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
929
930 return priv->shm;
931 }
932
933 GArray *
934 gst_wl_display_get_shm_formats (GstWlDisplay * self)
935 {
936 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
937
938 return priv->shm_formats;
939 }
940
941 struct zwp_linux_dmabuf_v1 *
942 gst_wl_display_get_dmabuf_v1 (GstWlDisplay * self)
943 {
944 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
945
946 return priv->dmabuf;
947 }
948
949 GArray *
950 gst_wl_display_get_dmabuf_modifiers (GstWlDisplay * self)
951 {
952 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
953
954 return priv->dmabuf_modifiers;
955 }
956
957 GArray *
958 gst_wl_display_get_dmabuf_formats (GstWlDisplay * self)
959 {
960 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
961
962 return priv->dmabuf_formats;
963 }
964
965 /**
966 * gst_wl_display_fill_shm_format_list:
967 * @self: A #GstWlDisplay
968 * @format_list: A #GValue of type #GST_TYPE_LIST
969 *
970 * Append supported SHM formats to a given list, suitable for use with the "format" caps value.
971 *
972 * Since: 1.26
973 */
974 void
975 gst_wl_display_fill_shm_format_list (GstWlDisplay * self, GValue * format_list)
976 {
977 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
978 GValue value = G_VALUE_INIT;
979 guint fmt;
980 GstVideoFormat gfmt;
981
982 for (gint i = 0; i < priv->shm_formats->len; i++) {
983 fmt = g_array_index (priv->shm_formats, uint32_t, i);
984 gfmt = gst_wl_shm_format_to_video_format (fmt);
985 if (gfmt != GST_VIDEO_FORMAT_UNKNOWN) {
986 g_value_init (&value, G_TYPE_STRING);
987 g_value_set_static_string (&value, gst_video_format_to_string (gfmt));
988 gst_value_list_append_and_take_value (format_list, &value);
989 }
990 }
991 }
992
993 /**
994 * gst_wl_display_fill_drm_format_list:
995 * @self: A #GstWlDisplay
996 * @format_list: A #GValue of type #GST_TYPE_LIST
997 *
998 * Append supported DRM formats to a given list, suitable for use with the "drm-format" caps value.
999 *
1000 * Since: 1.26
1001 */
1002 void
1003 gst_wl_display_fill_dmabuf_format_list (GstWlDisplay * self,
1004 GValue * format_list)
1005 {
1006 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
1007 GValue value = G_VALUE_INIT;
1008 guint fmt;
1009 guint64 mod;
1010
1011 for (gint i = 0; i < priv->dmabuf_formats->len; i++) {
1012 fmt = g_array_index (priv->dmabuf_formats, uint32_t, i);
1013 mod = g_array_index (priv->dmabuf_modifiers, guint64, i);
1014 g_value_init (&value, G_TYPE_STRING);
1015 g_value_take_string (&value, gst_video_dma_drm_fourcc_to_string (fmt, mod));
1016 gst_value_list_append_and_take_value (format_list, &value);
1017 }
1018 }
1019
1020 struct wp_single_pixel_buffer_manager_v1 *
1021 gst_wl_display_get_single_pixel_buffer_manager_v1 (GstWlDisplay * self)
1022 {
1023 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
1024
1025 return priv->single_pixel_buffer;
1026 }
1027
1028 gboolean
1029 gst_wl_display_has_own_display (GstWlDisplay * self)
1030 {
1031 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
1032
1033 return priv->own_display;
1034 }
1035
1036 /**
1037 * gst_wl_display_get_color_manager_v1:
1038 * @self: A #GstWlDisplay
1039 *
1040 * Returns: (transfer none): The color manager global or %NULL
1041 *
1042 * Since: 1.28
1043 */
1044 struct wp_color_manager_v1 *
1045 gst_wl_display_get_color_manager_v1 (GstWlDisplay * self)
1046 {
1047 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
1048
1049 return priv->color;
1050 }
1051
1052 /**
1053 * gst_wl_display_get_color_representation_manager_v1:
1054 * @self: A #GstWlDisplay
1055 *
1056 * Returns: (transfer none): The color representation global or %NULL
1057 *
1058 * Since: 1.28
1059 */
1060 struct wp_color_representation_manager_v1 *
1061 gst_wl_display_get_color_representation_manager_v1 (GstWlDisplay * self)
1062 {
1063 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
1064
1065 return priv->color_representation;
1066 }
1067
1068 /**
1069 * gst_wl_display_is_color_parametric_creator_supported:
1070 * @self: A #GstWlDisplay
1071 *
1072 * Returns: %TRUE if the compositor supports parametric image descriptions
1073 *
1074 * Since: 1.28
1075 */
1076 gboolean
1077 gst_wl_display_is_color_parametric_creator_supported (GstWlDisplay * self)
1078 {
1079 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
1080
1081 return priv->color_parametric_creator_supported;
1082 }
1083
1084 /**
1085 * gst_wl_display_is_color_mastering_display_supported:
1086 * @self: A #GstWlDisplay
1087 *
1088 * Returns: %TRUE if the compositor supports mastering display primaries
1089 * image descriptions
1090 *
1091 * Since: 1.28
1092 */
1093 gboolean
1094 gst_wl_display_is_color_mastering_display_supported (GstWlDisplay * self)
1095 {
1096 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
1097
1098 return priv->color_mastering_display_supported;
1099 }
1100
1101 /**
1102 * gst_wl_display_is_color_transfer_function_supported:
1103 * @self: A #GstWlDisplay
1104 *
1105 * Returns: %TRUE if the compositor supports @transfer_function
1106 *
1107 * Since: 1.28
1108 */
1109 gboolean
1110 gst_wl_display_is_color_transfer_function_supported (GstWlDisplay * self,
1111 uint32_t transfer_function)
1112 {
1113 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
1114 guint i;
1115
1116 /* A value of 0 is invalid and will never be present in the list of enums. */
1117 if (transfer_function == 0)
1118 return FALSE;
1119
1120 for (i = 0; i < priv->color_transfer_functions->len; i++) {
1121 uint32_t candidate =
1122 g_array_index (priv->color_transfer_functions, uint32_t, i);
1123
1124 if (candidate == transfer_function)
1125 return TRUE;
1126 }
1127
1128 return FALSE;
1129 }
1130
1131 /**
1132 * gst_wl_display_are_color_primaries_supported:
1133 * @self: A #GstWlDisplay
1134 *
1135 * Returns: %TRUE if the compositor supports @primaries
1136 *
1137 * Since: 1.28
1138 */
1139 gboolean
1140 gst_wl_display_are_color_primaries_supported (GstWlDisplay * self,
1141 uint32_t primaries)
1142 {
1143 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
1144 guint i;
1145
1146 /* A value of 0 is invalid and will never be present in the list of enums. */
1147 if (primaries == 0)
1148 return FALSE;
1149
1150 for (i = 0; i < priv->color_primaries->len; i++) {
1151 uint32_t candidate = g_array_index (priv->color_primaries, uint32_t, i);
1152
1153 if (candidate == primaries)
1154 return TRUE;
1155 }
1156
1157 return FALSE;
1158 }
1159
1160 /**
1161 * gst_wl_display_is_color_alpha_mode_supported:
1162 * @self: A #GstWlDisplay
1163 *
1164 * Returns: %TRUE if the compositor supports @alpha_mode
1165 *
1166 * Since: 1.28
1167 */
1168 gboolean
1169 gst_wl_display_is_color_alpha_mode_supported (GstWlDisplay * self,
1170 uint32_t alpha_mode)
1171 {
1172 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
1173 guint i;
1174
1175 for (i = 0; i < priv->color_alpha_modes->len; i++) {
1176 uint32_t candidate = g_array_index (priv->color_alpha_modes, uint32_t, i);
1177
1178 if (candidate == alpha_mode)
1179 return TRUE;
1180 }
1181
1182 return FALSE;
1183 }
1184
1185 /**
1186 * gst_wl_display_are_color_coefficients_supported:
1187 * @self: A #GstWlDisplay
1188 *
1189 * Returns: %TRUE if the compositor supports the combination of @coefficients and @range
1190 *
1191 * Since: 1.28
1192 */
1193 gboolean
1194 gst_wl_display_are_color_coefficients_supported (GstWlDisplay * self,
1195 uint32_t coefficients, uint32_t range)
1196 {
1197 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
1198 guint i;
1199
1200 /* A value of 0 is invalid and will never be present in the list of enums. */
1201 if (coefficients == 0 || range == 0)
1202 return FALSE;
1203
1204 for (i = 0; i < priv->color_coefficients->len; i++) {
1205 uint32_t candidate = g_array_index (priv->color_coefficients, uint32_t, i);
1206 uint32_t candidate_range =
1207 g_array_index (priv->color_coefficients_range, uint32_t, i);
1208
1209 if (candidate == coefficients && candidate_range == range)
1210 return TRUE;
1211 }
1212
1213 return FALSE;
1214 }
1215
1216 /**
1217 * gst_wl_display_get_output_by_name:
1218 * @self: A #GstWlDisplay
1219 * @output_name: Name of the output
1220 *
1221 * Lookup for a wl_output with the specified name.
1222 *
1223 * Returns: (transfer full): A #GstWlOutput or %NULL if not found.
1224 *
1225 * Since: 1.28
1226 */
1227 GstWlOutput *
1228 gst_wl_display_get_output_by_name (GstWlDisplay * self,
1229 const gchar * output_name)
1230 {
1231 GstWlDisplayPrivate *priv = gst_wl_display_get_instance_private (self);
1232 GstWlOutput *output;
1233
1234 g_mutex_lock (&priv->outputs_mutex);
1235 output = GST_WL_OUTPUT (g_hash_table_lookup (priv->outputs, output_name));
1236 if (output)
1237 g_object_ref (output);
1238 g_mutex_unlock (&priv->outputs_mutex);
1239
1240 return output;
1241 }