1 /* GStreamer Wayland video sink
3 * Copyright (C) 2011 Intel Corporation
4 * Copyright (C) 2011 Sreerenj Balachandran <sreerenj.balachandran@intel.com>
5 * Copyright (C) 2014 Collabora Ltd.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301 USA.
28 #include "wlshmallocator.h"
31 GST_DEBUG_CATEGORY_EXTERN (gstwayland_debug);
32 #define GST_CAT_DEFAULT gstwayland_debug
40 static guint signals[LAST_SIGNAL] = { 0 };
42 G_DEFINE_TYPE (GstWlWindow, gst_wl_window, G_TYPE_OBJECT);
44 static void gst_wl_window_finalize (GObject * gobject);
47 handle_xdg_toplevel_close (void *data, struct xdg_toplevel *xdg_toplevel)
49 GstWlWindow *window = data;
51 GST_DEBUG ("XDG toplevel got a \"close\" event.");
52 g_signal_emit (window, signals[CLOSED], 0);
56 handle_xdg_toplevel_configure (void *data, struct xdg_toplevel *xdg_toplevel,
57 int32_t width, int32_t height, struct wl_array *states)
59 GstWlWindow *window = data;
60 const uint32_t *state;
62 GST_DEBUG ("XDG toplevel got a \"configure\" event, [ %d, %d ].",
65 wl_array_for_each (state, states) {
67 case XDG_TOPLEVEL_STATE_FULLSCREEN:
68 case XDG_TOPLEVEL_STATE_MAXIMIZED:
69 case XDG_TOPLEVEL_STATE_RESIZING:
70 case XDG_TOPLEVEL_STATE_ACTIVATED:
75 if (width <= 0 || height <= 0)
78 gst_wl_window_set_render_rectangle (window, 0, 0, width, height);
81 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
82 handle_xdg_toplevel_configure,
83 handle_xdg_toplevel_close,
87 handle_xdg_surface_configure (void *data, struct xdg_surface *xdg_surface,
90 GstWlWindow *window = data;
91 xdg_surface_ack_configure (xdg_surface, serial);
93 g_mutex_lock (&window->configure_mutex);
94 window->configured = TRUE;
95 g_cond_signal (&window->configure_cond);
96 g_mutex_unlock (&window->configure_mutex);
99 static const struct xdg_surface_listener xdg_surface_listener = {
100 handle_xdg_surface_configure,
104 handle_ping (void *data, struct wl_shell_surface *wl_shell_surface,
107 wl_shell_surface_pong (wl_shell_surface, serial);
111 handle_configure (void *data, struct wl_shell_surface *wl_shell_surface,
112 uint32_t edges, int32_t width, int32_t height)
114 GstWlWindow *window = data;
116 GST_DEBUG ("Windows configure: edges %x, width = %i, height %i", edges,
119 if (width == 0 || height == 0)
122 gst_wl_window_set_render_rectangle (window, 0, 0, width, height);
126 handle_popup_done (void *data, struct wl_shell_surface *wl_shell_surface)
128 GST_DEBUG ("Window popup done.");
131 static const struct wl_shell_surface_listener wl_shell_surface_listener = {
138 gst_wl_window_class_init (GstWlWindowClass * klass)
140 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
141 gobject_class->finalize = gst_wl_window_finalize;
143 signals[CLOSED] = g_signal_new ("closed", G_TYPE_FROM_CLASS (gobject_class),
144 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
148 gst_wl_window_init (GstWlWindow * self)
150 self->configured = TRUE;
151 g_cond_init (&self->configure_cond);
152 g_mutex_init (&self->configure_mutex);
156 gst_wl_window_finalize (GObject * gobject)
158 GstWlWindow *self = GST_WL_WINDOW (gobject);
160 if (self->wl_shell_surface)
161 wl_shell_surface_destroy (self->wl_shell_surface);
163 if (self->xdg_toplevel)
164 xdg_toplevel_destroy (self->xdg_toplevel);
165 if (self->xdg_surface)
166 xdg_surface_destroy (self->xdg_surface);
168 if (self->video_viewport)
169 wp_viewport_destroy (self->video_viewport);
171 wl_proxy_wrapper_destroy (self->video_surface_wrapper);
172 wl_subsurface_destroy (self->video_subsurface);
173 wl_surface_destroy (self->video_surface);
175 if (self->area_subsurface)
176 wl_subsurface_destroy (self->area_subsurface);
178 if (self->area_viewport)
179 wp_viewport_destroy (self->area_viewport);
181 wl_proxy_wrapper_destroy (self->area_surface_wrapper);
182 wl_surface_destroy (self->area_surface);
184 g_clear_object (&self->display);
186 G_OBJECT_CLASS (gst_wl_window_parent_class)->finalize (gobject);
190 gst_wl_window_new_internal (GstWlDisplay * display, GMutex * render_lock)
194 window = g_object_new (GST_TYPE_WL_WINDOW, NULL);
195 window->display = g_object_ref (display);
196 window->render_lock = render_lock;
197 g_cond_init (&window->configure_cond);
199 window->area_surface = wl_compositor_create_surface (display->compositor);
200 window->video_surface = wl_compositor_create_surface (display->compositor);
202 window->area_surface_wrapper = wl_proxy_create_wrapper (window->area_surface);
203 window->video_surface_wrapper =
204 wl_proxy_create_wrapper (window->video_surface);
206 wl_proxy_set_queue ((struct wl_proxy *) window->area_surface_wrapper,
208 wl_proxy_set_queue ((struct wl_proxy *) window->video_surface_wrapper,
211 /* embed video_surface in area_surface */
212 window->video_subsurface =
213 wl_subcompositor_get_subsurface (display->subcompositor,
214 window->video_surface, window->area_surface);
215 wl_subsurface_set_desync (window->video_subsurface);
217 if (display->viewporter) {
218 window->area_viewport = wp_viewporter_get_viewport (display->viewporter,
219 window->area_surface);
220 window->video_viewport = wp_viewporter_get_viewport (display->viewporter,
221 window->video_surface);
224 /* do not accept input */
225 wl_surface_set_input_region (window->area_surface, NULL);
226 wl_surface_set_input_region (window->video_surface, NULL);
232 gst_wl_window_ensure_fullscreen (GstWlWindow * window, gboolean fullscreen)
237 if (window->display->xdg_wm_base) {
239 xdg_toplevel_set_fullscreen (window->xdg_toplevel, NULL);
241 xdg_toplevel_unset_fullscreen (window->xdg_toplevel);
244 wl_shell_surface_set_fullscreen (window->wl_shell_surface,
245 WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE, 0, NULL);
247 wl_shell_surface_set_toplevel (window->wl_shell_surface);
252 gst_wl_window_new_toplevel (GstWlDisplay * display, const GstVideoInfo * info,
253 gboolean fullscreen, GMutex * render_lock)
258 window = gst_wl_window_new_internal (display, render_lock);
260 /* Check which protocol we will use (in order of preference) */
261 if (display->xdg_wm_base) {
264 /* First create the XDG surface */
265 window->xdg_surface = xdg_wm_base_get_xdg_surface (display->xdg_wm_base,
266 window->area_surface);
267 if (!window->xdg_surface) {
268 GST_ERROR ("Unable to get xdg_surface");
271 xdg_surface_add_listener (window->xdg_surface, &xdg_surface_listener,
274 /* Then the toplevel */
275 window->xdg_toplevel = xdg_surface_get_toplevel (window->xdg_surface);
276 if (!window->xdg_toplevel) {
277 GST_ERROR ("Unable to get xdg_toplevel");
280 xdg_toplevel_add_listener (window->xdg_toplevel,
281 &xdg_toplevel_listener, window);
283 gst_wl_window_ensure_fullscreen (window, fullscreen);
285 /* Finally, commit the xdg_surface state as toplevel */
286 window->configured = FALSE;
287 wl_surface_commit (window->video_surface);
288 wl_display_flush (display->display);
290 g_mutex_lock (&window->configure_mutex);
291 timeout = g_get_monotonic_time () + 100 * G_TIME_SPAN_MILLISECOND;
292 while (!window->configured) {
293 if (!g_cond_wait_until (&window->configure_cond, &window->configure_mutex,
295 GST_WARNING ("The compositor did not send configure event.");
299 g_mutex_unlock (&window->configure_mutex);
300 } else if (display->wl_shell) {
302 window->wl_shell_surface = wl_shell_get_shell_surface (display->wl_shell,
303 window->area_surface);
304 if (!window->wl_shell_surface) {
305 GST_ERROR ("Unable to get wl_shell_surface");
309 wl_shell_surface_add_listener (window->wl_shell_surface,
310 &wl_shell_surface_listener, window);
311 gst_wl_window_ensure_fullscreen (window, fullscreen);
312 } else if (display->fullscreen_shell) {
313 zwp_fullscreen_shell_v1_present_surface (display->fullscreen_shell,
314 window->area_surface, ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_ZOOM,
317 GST_ERROR ("Unable to use either wl_shell, xdg_wm_base or "
318 "zwp_fullscreen_shell.");
322 /* set the initial size to be the same as the reported video size */
324 gst_util_uint64_scale_int_round (info->width, info->par_n, info->par_d);
325 gst_wl_window_set_render_rectangle (window, 0, 0, width, info->height);
330 g_object_unref (window);
335 gst_wl_window_new_in_surface (GstWlDisplay * display,
336 struct wl_surface * parent, GMutex * render_lock)
339 window = gst_wl_window_new_internal (display, render_lock);
341 /* embed in parent */
342 window->area_subsurface =
343 wl_subcompositor_get_subsurface (display->subcompositor,
344 window->area_surface, parent);
345 wl_subsurface_set_desync (window->area_subsurface);
351 gst_wl_window_get_display (GstWlWindow * window)
353 g_return_val_if_fail (window != NULL, NULL);
355 return g_object_ref (window->display);
359 gst_wl_window_get_wl_surface (GstWlWindow * window)
361 g_return_val_if_fail (window != NULL, NULL);
363 return window->video_surface_wrapper;
367 gst_wl_window_is_toplevel (GstWlWindow * window)
369 g_return_val_if_fail (window != NULL, FALSE);
371 if (window->display->xdg_wm_base)
372 return (window->xdg_toplevel != NULL);
374 return (window->wl_shell_surface != NULL);
378 gst_wl_window_resize_video_surface (GstWlWindow * window, gboolean commit)
380 GstVideoRectangle src = { 0, };
381 GstVideoRectangle dst = { 0, };
382 GstVideoRectangle res;
384 /* center the video_subsurface inside area_subsurface */
385 src.w = window->video_width;
386 src.h = window->video_height;
387 dst.w = window->render_rectangle.w;
388 dst.h = window->render_rectangle.h;
390 if (window->video_viewport) {
391 gst_video_sink_center_rect (src, dst, &res, TRUE);
392 wp_viewport_set_destination (window->video_viewport, res.w, res.h);
394 gst_video_sink_center_rect (src, dst, &res, FALSE);
397 wl_subsurface_set_position (window->video_subsurface, res.x, res.y);
400 wl_surface_damage (window->video_surface_wrapper, 0, 0, res.w, res.h);
401 wl_surface_commit (window->video_surface_wrapper);
404 if (gst_wl_window_is_toplevel (window)) {
405 struct wl_region *region;
407 region = wl_compositor_create_region (window->display->compositor);
408 wl_region_add (region, 0, 0, window->render_rectangle.w,
409 window->render_rectangle.h);
410 wl_surface_set_input_region (window->area_surface, region);
411 wl_region_destroy (region);
414 /* this is saved for use in wl_surface_damage */
415 window->video_rectangle = res;
419 gst_wl_window_set_opaque (GstWlWindow * window, const GstVideoInfo * info)
421 struct wl_region *region;
423 /* Set area opaque */
424 region = wl_compositor_create_region (window->display->compositor);
425 wl_region_add (region, 0, 0, window->render_rectangle.w,
426 window->render_rectangle.h);
427 wl_surface_set_opaque_region (window->area_surface, region);
428 wl_region_destroy (region);
430 if (!GST_VIDEO_INFO_HAS_ALPHA (info)) {
431 /* Set video opaque */
432 region = wl_compositor_create_region (window->display->compositor);
433 wl_region_add (region, 0, 0, window->render_rectangle.w,
434 window->render_rectangle.h);
435 wl_surface_set_opaque_region (window->video_surface, region);
436 wl_region_destroy (region);
441 gst_wl_window_render (GstWlWindow * window, GstWlBuffer * buffer,
442 const GstVideoInfo * info)
444 if (G_UNLIKELY (info)) {
445 window->video_width =
446 gst_util_uint64_scale_int_round (info->width, info->par_n, info->par_d);
447 window->video_height = info->height;
449 wl_subsurface_set_sync (window->video_subsurface);
450 gst_wl_window_resize_video_surface (window, FALSE);
451 gst_wl_window_set_opaque (window, info);
454 if (G_LIKELY (buffer))
455 gst_wl_buffer_attach (buffer, window->video_surface_wrapper);
457 wl_surface_attach (window->video_surface_wrapper, NULL, 0, 0);
459 wl_surface_damage (window->video_surface_wrapper, 0, 0,
460 window->video_rectangle.w, window->video_rectangle.h);
461 wl_surface_commit (window->video_surface_wrapper);
463 if (G_UNLIKELY (info)) {
464 /* commit also the parent (area_surface) in order to change
465 * the position of the video_subsurface */
466 wl_surface_damage (window->area_surface_wrapper, 0, 0,
467 window->render_rectangle.w, window->render_rectangle.h);
468 wl_surface_commit (window->area_surface_wrapper);
469 wl_subsurface_set_desync (window->video_subsurface);
472 wl_display_flush (window->display->display);
475 /* Update the buffer used to draw black borders. When we have viewporter
476 * support, this is a scaled up 1x1 image, and without we need an black image
477 * the size of the rendering areay. */
479 gst_wl_window_update_borders (GstWlWindow * window)
481 GstVideoFormat format;
485 struct wl_buffer *wlbuf;
489 if (window->no_border_update)
492 if (window->display->viewporter) {
494 window->no_border_update = TRUE;
496 width = window->render_rectangle.w;
497 height = window->render_rectangle.h;
500 /* we want WL_SHM_FORMAT_XRGB8888 */
501 #if G_BYTE_ORDER == G_BIG_ENDIAN
502 format = GST_VIDEO_FORMAT_xRGB;
504 format = GST_VIDEO_FORMAT_BGRx;
507 /* draw the area_subsurface */
508 gst_video_info_set_format (&info, format, width, height);
510 alloc = gst_wl_shm_allocator_get ();
512 buf = gst_buffer_new_allocate (alloc, info.size, NULL);
513 gst_buffer_memset (buf, 0, 0, info.size);
515 gst_wl_shm_memory_construct_wl_buffer (gst_buffer_peek_memory (buf, 0),
516 window->display, &info);
517 gwlbuf = gst_buffer_add_wl_buffer (buf, wlbuf, window->display);
518 gst_wl_buffer_attach (gwlbuf, window->area_surface_wrapper);
520 /* at this point, the GstWlBuffer keeps the buffer
521 * alive and will free it on wl_buffer::release */
522 gst_buffer_unref (buf);
523 g_object_unref (alloc);
527 gst_wl_window_set_render_rectangle (GstWlWindow * window, gint x, gint y,
530 g_return_if_fail (window != NULL);
532 window->render_rectangle.x = x;
533 window->render_rectangle.y = y;
534 window->render_rectangle.w = w;
535 window->render_rectangle.h = h;
537 /* position the area inside the parent - needs a parent commit to apply */
538 if (window->area_subsurface)
539 wl_subsurface_set_position (window->area_subsurface, x, y);
541 /* change the size of the area */
542 if (window->area_viewport)
543 wp_viewport_set_destination (window->area_viewport, w, h);
545 gst_wl_window_update_borders (window);
547 if (window->video_width != 0) {
548 wl_subsurface_set_sync (window->video_subsurface);
549 gst_wl_window_resize_video_surface (window, TRUE);
552 wl_surface_damage (window->area_surface_wrapper, 0, 0, w, h);
553 wl_surface_commit (window->area_surface_wrapper);
555 if (window->video_width != 0)
556 wl_subsurface_set_desync (window->video_subsurface);