1 /* GStreamer Wayland video sink
2 *
3 * Copyright (C) 2011 Intel Corporation
4 * Copyright (C) 2011 Sreerenj Balachandran <sreerenj.balachandran@intel.com>
5 * Copyright (C) 2014 Collabora Ltd.
6 *
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.
11 *
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.
16 *
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.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "wlwindow.h"
28 #include "wlshmallocator.h"
29 #include "wlbuffer.h"
30
31 GST_DEBUG_CATEGORY_EXTERN (gstwayland_debug);
32 #define GST_CAT_DEFAULT gstwayland_debug
33
34 enum
35 {
36 CLOSED,
37 LAST_SIGNAL
38 };
39
40 static guint signals[LAST_SIGNAL] = { 0 };
41
42 G_DEFINE_TYPE (GstWlWindow, gst_wl_window, G_TYPE_OBJECT);
43
44 static void gst_wl_window_finalize (GObject * gobject);
45
46 static void gst_wl_window_update_borders (GstWlWindow * window);
47
48 static void
handle_xdg_toplevel_close(void * data,struct xdg_toplevel * xdg_toplevel)49 handle_xdg_toplevel_close (void *data, struct xdg_toplevel *xdg_toplevel)
50 {
51 GstWlWindow *window = data;
52
53 GST_DEBUG ("XDG toplevel got a \"close\" event.");
54 g_signal_emit (window, signals[CLOSED], 0);
55 }
56
57 static void
handle_xdg_toplevel_configure(void * data,struct xdg_toplevel * xdg_toplevel,int32_t width,int32_t height,struct wl_array * states)58 handle_xdg_toplevel_configure (void *data, struct xdg_toplevel *xdg_toplevel,
59 int32_t width, int32_t height, struct wl_array *states)
60 {
61 GstWlWindow *window = data;
62 const uint32_t *state;
63
64 GST_DEBUG ("XDG toplevel got a \"configure\" event, [ %d, %d ].",
65 width, height);
66
67 wl_array_for_each (state, states) {
68 switch (*state) {
69 case XDG_TOPLEVEL_STATE_FULLSCREEN:
70 case XDG_TOPLEVEL_STATE_MAXIMIZED:
71 case XDG_TOPLEVEL_STATE_RESIZING:
72 case XDG_TOPLEVEL_STATE_ACTIVATED:
73 break;
74 }
75 }
76
77 if (width <= 0 || height <= 0)
78 return;
79
80 gst_wl_window_set_render_rectangle (window, 0, 0, width, height);
81 }
82
83 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
84 handle_xdg_toplevel_configure,
85 handle_xdg_toplevel_close,
86 };
87
88 static void
handle_xdg_surface_configure(void * data,struct xdg_surface * xdg_surface,uint32_t serial)89 handle_xdg_surface_configure (void *data, struct xdg_surface *xdg_surface,
90 uint32_t serial)
91 {
92 GstWlWindow *window = data;
93 xdg_surface_ack_configure (xdg_surface, serial);
94
95 g_mutex_lock (&window->configure_mutex);
96 window->configured = TRUE;
97 g_cond_signal (&window->configure_cond);
98 g_mutex_unlock (&window->configure_mutex);
99 }
100
101 static const struct xdg_surface_listener xdg_surface_listener = {
102 handle_xdg_surface_configure,
103 };
104
105 static void
handle_ping(void * data,struct wl_shell_surface * wl_shell_surface,uint32_t serial)106 handle_ping (void *data, struct wl_shell_surface *wl_shell_surface,
107 uint32_t serial)
108 {
109 wl_shell_surface_pong (wl_shell_surface, serial);
110 }
111
112 static void
handle_configure(void * data,struct wl_shell_surface * wl_shell_surface,uint32_t edges,int32_t width,int32_t height)113 handle_configure (void *data, struct wl_shell_surface *wl_shell_surface,
114 uint32_t edges, int32_t width, int32_t height)
115 {
116 GstWlWindow *window = data;
117
118 GST_DEBUG ("Windows configure: edges %x, width = %i, height %i", edges,
119 width, height);
120
121 if (width == 0 || height == 0)
122 return;
123
124 gst_wl_window_set_render_rectangle (window, 0, 0, width, height);
125 }
126
127 static void
handle_popup_done(void * data,struct wl_shell_surface * wl_shell_surface)128 handle_popup_done (void *data, struct wl_shell_surface *wl_shell_surface)
129 {
130 GST_DEBUG ("Window popup done.");
131 }
132
133 static const struct wl_shell_surface_listener wl_shell_surface_listener = {
134 handle_ping,
135 handle_configure,
136 handle_popup_done
137 };
138
139 static void
gst_wl_window_class_init(GstWlWindowClass * klass)140 gst_wl_window_class_init (GstWlWindowClass * klass)
141 {
142 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
143 gobject_class->finalize = gst_wl_window_finalize;
144
145 signals[CLOSED] = g_signal_new ("closed", G_TYPE_FROM_CLASS (gobject_class),
146 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
147 }
148
149 static void
gst_wl_window_init(GstWlWindow * self)150 gst_wl_window_init (GstWlWindow * self)
151 {
152 self->configured = TRUE;
153 g_cond_init (&self->configure_cond);
154 g_mutex_init (&self->configure_mutex);
155 }
156
157 static void
gst_wl_window_finalize(GObject * gobject)158 gst_wl_window_finalize (GObject * gobject)
159 {
160 GstWlWindow *self = GST_WL_WINDOW (gobject);
161
162 if (self->wl_shell_surface)
163 wl_shell_surface_destroy (self->wl_shell_surface);
164
165 if (self->xdg_toplevel)
166 xdg_toplevel_destroy (self->xdg_toplevel);
167 if (self->xdg_surface)
168 xdg_surface_destroy (self->xdg_surface);
169
170 if (self->video_viewport)
171 wp_viewport_destroy (self->video_viewport);
172
173 wl_proxy_wrapper_destroy (self->video_surface_wrapper);
174 wl_subsurface_destroy (self->video_subsurface);
175 wl_surface_destroy (self->video_surface);
176
177 if (self->area_subsurface)
178 wl_subsurface_destroy (self->area_subsurface);
179
180 if (self->area_viewport)
181 wp_viewport_destroy (self->area_viewport);
182
183 wl_proxy_wrapper_destroy (self->area_surface_wrapper);
184 wl_surface_destroy (self->area_surface);
185
186 g_clear_object (&self->display);
187
188 G_OBJECT_CLASS (gst_wl_window_parent_class)->finalize (gobject);
189 }
190
191 static GstWlWindow *
gst_wl_window_new_internal(GstWlDisplay * display,GMutex * render_lock)192 gst_wl_window_new_internal (GstWlDisplay * display, GMutex * render_lock)
193 {
194 GstWlWindow *window;
195 struct wl_region *region;
196
197 window = g_object_new (GST_TYPE_WL_WINDOW, NULL);
198 window->display = g_object_ref (display);
199 window->render_lock = render_lock;
200 g_cond_init (&window->configure_cond);
201
202 window->area_surface = wl_compositor_create_surface (display->compositor);
203 window->video_surface = wl_compositor_create_surface (display->compositor);
204
205 window->area_surface_wrapper = wl_proxy_create_wrapper (window->area_surface);
206 window->video_surface_wrapper =
207 wl_proxy_create_wrapper (window->video_surface);
208
209 wl_proxy_set_queue ((struct wl_proxy *) window->area_surface_wrapper,
210 display->queue);
211 wl_proxy_set_queue ((struct wl_proxy *) window->video_surface_wrapper,
212 display->queue);
213
214 /* embed video_surface in area_surface */
215 window->video_subsurface =
216 wl_subcompositor_get_subsurface (display->subcompositor,
217 window->video_surface, window->area_surface);
218 wl_subsurface_set_desync (window->video_subsurface);
219
220 if (display->viewporter) {
221 window->area_viewport = wp_viewporter_get_viewport (display->viewporter,
222 window->area_surface);
223 window->video_viewport = wp_viewporter_get_viewport (display->viewporter,
224 window->video_surface);
225 }
226
227 /* never accept input events on the video surface */
228 region = wl_compositor_create_region (display->compositor);
229 wl_surface_set_input_region (window->video_surface, region);
230 wl_region_destroy (region);
231
232 return window;
233 }
234
235 void
gst_wl_window_ensure_fullscreen(GstWlWindow * window,gboolean fullscreen)236 gst_wl_window_ensure_fullscreen (GstWlWindow * window, gboolean fullscreen)
237 {
238 if (!window)
239 return;
240
241 if (window->display->xdg_wm_base) {
242 if (fullscreen)
243 xdg_toplevel_set_fullscreen (window->xdg_toplevel, NULL);
244 else
245 xdg_toplevel_unset_fullscreen (window->xdg_toplevel);
246 } else {
247 if (fullscreen)
248 wl_shell_surface_set_fullscreen (window->wl_shell_surface,
249 WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE, 0, NULL);
250 else
251 wl_shell_surface_set_toplevel (window->wl_shell_surface);
252 }
253 }
254
255 GstWlWindow *
gst_wl_window_new_toplevel(GstWlDisplay * display,const GstVideoInfo * info,gboolean fullscreen,GMutex * render_lock)256 gst_wl_window_new_toplevel (GstWlDisplay * display, const GstVideoInfo * info,
257 gboolean fullscreen, GMutex * render_lock)
258 {
259 GstWlWindow *window;
260
261 window = gst_wl_window_new_internal (display, render_lock);
262
263 /* Check which protocol we will use (in order of preference) */
264 if (display->xdg_wm_base) {
265 gint64 timeout;
266
267 /* First create the XDG surface */
268 window->xdg_surface = xdg_wm_base_get_xdg_surface (display->xdg_wm_base,
269 window->area_surface);
270 if (!window->xdg_surface) {
271 GST_ERROR ("Unable to get xdg_surface");
272 goto error;
273 }
274 xdg_surface_add_listener (window->xdg_surface, &xdg_surface_listener,
275 window);
276
277 /* Then the toplevel */
278 window->xdg_toplevel = xdg_surface_get_toplevel (window->xdg_surface);
279 if (!window->xdg_toplevel) {
280 GST_ERROR ("Unable to get xdg_toplevel");
281 goto error;
282 }
283 xdg_toplevel_add_listener (window->xdg_toplevel,
284 &xdg_toplevel_listener, window);
285
286 gst_wl_window_ensure_fullscreen (window, fullscreen);
287
288 /* Finally, commit the xdg_surface state as toplevel */
289 window->configured = FALSE;
290 wl_surface_commit (window->area_surface);
291 wl_display_flush (display->display);
292
293 g_mutex_lock (&window->configure_mutex);
294 timeout = g_get_monotonic_time () + 100 * G_TIME_SPAN_MILLISECOND;
295 while (!window->configured) {
296 if (!g_cond_wait_until (&window->configure_cond, &window->configure_mutex,
297 timeout)) {
298 GST_WARNING ("The compositor did not send configure event.");
299 break;
300 }
301 }
302 g_mutex_unlock (&window->configure_mutex);
303 } else if (display->wl_shell) {
304 /* go toplevel */
305 window->wl_shell_surface = wl_shell_get_shell_surface (display->wl_shell,
306 window->area_surface);
307 if (!window->wl_shell_surface) {
308 GST_ERROR ("Unable to get wl_shell_surface");
309 goto error;
310 }
311
312 wl_shell_surface_add_listener (window->wl_shell_surface,
313 &wl_shell_surface_listener, window);
314 gst_wl_window_ensure_fullscreen (window, fullscreen);
315 } else if (display->fullscreen_shell) {
316 zwp_fullscreen_shell_v1_present_surface (display->fullscreen_shell,
317 window->area_surface, ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_ZOOM,
318 NULL);
319 } else {
320 GST_ERROR ("Unable to use either wl_shell, xdg_wm_base or "
321 "zwp_fullscreen_shell.");
322 goto error;
323 }
324
325 /* render_rectangle is already set via toplevel_configure in
326 * xdg_shell fullscreen mode */
327 if (!(display->xdg_wm_base && fullscreen)) {
328 /* set the initial size to be the same as the reported video size */
329 gint width =
330 gst_util_uint64_scale_int_round (info->width, info->par_n, info->par_d);
331 gst_wl_window_set_render_rectangle (window, 0, 0, width, info->height);
332 }
333
334 return window;
335
336 error:
337 g_object_unref (window);
338 return NULL;
339 }
340
341 GstWlWindow *
gst_wl_window_new_in_surface(GstWlDisplay * display,struct wl_surface * parent,GMutex * render_lock)342 gst_wl_window_new_in_surface (GstWlDisplay * display,
343 struct wl_surface * parent, GMutex * render_lock)
344 {
345 GstWlWindow *window;
346 struct wl_region *region;
347 window = gst_wl_window_new_internal (display, render_lock);
348
349 /* do not accept input events on the area surface when embedded */
350 region = wl_compositor_create_region (display->compositor);
351 wl_surface_set_input_region (window->area_surface, region);
352 wl_region_destroy (region);
353
354 /* embed in parent */
355 window->area_subsurface =
356 wl_subcompositor_get_subsurface (display->subcompositor,
357 window->area_surface, parent);
358 wl_subsurface_set_desync (window->area_subsurface);
359
360 wl_surface_commit (parent);
361
362 return window;
363 }
364
365 GstWlDisplay *
gst_wl_window_get_display(GstWlWindow * window)366 gst_wl_window_get_display (GstWlWindow * window)
367 {
368 g_return_val_if_fail (window != NULL, NULL);
369
370 return g_object_ref (window->display);
371 }
372
373 struct wl_surface *
gst_wl_window_get_wl_surface(GstWlWindow * window)374 gst_wl_window_get_wl_surface (GstWlWindow * window)
375 {
376 g_return_val_if_fail (window != NULL, NULL);
377
378 return window->video_surface_wrapper;
379 }
380
381 gboolean
gst_wl_window_is_toplevel(GstWlWindow * window)382 gst_wl_window_is_toplevel (GstWlWindow * window)
383 {
384 g_return_val_if_fail (window != NULL, FALSE);
385
386 if (window->display->xdg_wm_base)
387 return (window->xdg_toplevel != NULL);
388 else
389 return (window->wl_shell_surface != NULL);
390 }
391
392 static void
gst_wl_window_resize_video_surface(GstWlWindow * window,gboolean commit)393 gst_wl_window_resize_video_surface (GstWlWindow * window, gboolean commit)
394 {
395 GstVideoRectangle src = { 0, };
396 GstVideoRectangle dst = { 0, };
397 GstVideoRectangle res;
398
399 /* center the video_subsurface inside area_subsurface */
400 src.w = window->video_width;
401 src.h = window->video_height;
402 dst.w = window->render_rectangle.w;
403 dst.h = window->render_rectangle.h;
404
405 if (window->video_viewport) {
406 gst_video_sink_center_rect (src, dst, &res, TRUE);
407 wp_viewport_set_destination (window->video_viewport, res.w, res.h);
408 } else {
409 gst_video_sink_center_rect (src, dst, &res, FALSE);
410 }
411
412 wl_subsurface_set_position (window->video_subsurface, res.x, res.y);
413
414 if (commit)
415 wl_surface_commit (window->video_surface_wrapper);
416
417 window->video_rectangle = res;
418 }
419
420 static void
gst_wl_window_set_opaque(GstWlWindow * window,const GstVideoInfo * info)421 gst_wl_window_set_opaque (GstWlWindow * window, const GstVideoInfo * info)
422 {
423 struct wl_region *region;
424
425 /* Set area opaque */
426 region = wl_compositor_create_region (window->display->compositor);
427 wl_region_add (region, 0, 0, G_MAXINT32, G_MAXINT32);
428 wl_surface_set_opaque_region (window->area_surface, region);
429 wl_region_destroy (region);
430
431 if (!GST_VIDEO_INFO_HAS_ALPHA (info)) {
432 /* Set video opaque */
433 region = wl_compositor_create_region (window->display->compositor);
434 wl_region_add (region, 0, 0, G_MAXINT32, G_MAXINT32);
435 wl_surface_set_opaque_region (window->video_surface, region);
436 wl_region_destroy (region);
437 }
438 }
439
440 void
gst_wl_window_render(GstWlWindow * window,GstWlBuffer * buffer,const GstVideoInfo * info)441 gst_wl_window_render (GstWlWindow * window, GstWlBuffer * buffer,
442 const GstVideoInfo * info)
443 {
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;
448
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);
452 }
453
454 if (G_LIKELY (buffer)) {
455 gst_wl_buffer_attach (buffer, window->video_surface_wrapper);
456 wl_surface_damage_buffer (window->video_surface_wrapper, 0, 0, G_MAXINT32,
457 G_MAXINT32);
458 wl_surface_commit (window->video_surface_wrapper);
459
460 if (!window->is_area_surface_mapped) {
461 gst_wl_window_update_borders (window);
462 wl_surface_commit (window->area_surface_wrapper);
463 window->is_area_surface_mapped = TRUE;
464 }
465 } else {
466 /* clear both video and parent surfaces */
467 wl_surface_attach (window->video_surface_wrapper, NULL, 0, 0);
468 wl_surface_commit (window->video_surface_wrapper);
469 wl_surface_attach (window->area_surface_wrapper, NULL, 0, 0);
470 wl_surface_commit (window->area_surface_wrapper);
471 window->is_area_surface_mapped = FALSE;
472 }
473
474 if (G_UNLIKELY (info)) {
475 /* commit also the parent (area_surface) in order to change
476 * the position of the video_subsurface */
477 wl_surface_commit (window->area_surface_wrapper);
478 wl_subsurface_set_desync (window->video_subsurface);
479 }
480
481 wl_display_flush (window->display->display);
482 }
483
484 /* Update the buffer used to draw black borders. When we have viewporter
485 * support, this is a scaled up 1x1 image, and without we need an black image
486 * the size of the rendering areay. */
487 static void
gst_wl_window_update_borders(GstWlWindow * window)488 gst_wl_window_update_borders (GstWlWindow * window)
489 {
490 GstVideoFormat format;
491 GstVideoInfo info;
492 gint width, height;
493 GstBuffer *buf;
494 struct wl_buffer *wlbuf;
495 GstWlBuffer *gwlbuf;
496 GstAllocator *alloc;
497
498 if (window->display->viewporter) {
499 wp_viewport_set_destination (window->area_viewport,
500 window->render_rectangle.w, window->render_rectangle.h);
501
502 if (window->is_area_surface_mapped) {
503 /* The area_surface is already visible and only needed to get resized.
504 * We don't need to attach a new buffer and are done here. */
505 return;
506 }
507 }
508
509 if (window->display->viewporter) {
510 width = height = 1;
511 } else {
512 width = window->render_rectangle.w;
513 height = window->render_rectangle.h;
514 }
515
516 /* we want WL_SHM_FORMAT_XRGB8888 */
517 format = GST_VIDEO_FORMAT_BGRx;
518
519 /* draw the area_subsurface */
520 gst_video_info_set_format (&info, format, width, height);
521
522 alloc = gst_wl_shm_allocator_get ();
523
524 buf = gst_buffer_new_allocate (alloc, info.size, NULL);
525 gst_buffer_memset (buf, 0, 0, info.size);
526 wlbuf =
527 gst_wl_shm_memory_construct_wl_buffer (gst_buffer_peek_memory (buf, 0),
528 window->display, &info);
529 gwlbuf = gst_buffer_add_wl_buffer (buf, wlbuf, window->display);
530 gst_wl_buffer_attach (gwlbuf, window->area_surface_wrapper);
531 wl_surface_damage_buffer (window->area_surface_wrapper, 0, 0, G_MAXINT32,
532 G_MAXINT32);
533
534 /* at this point, the GstWlBuffer keeps the buffer
535 * alive and will free it on wl_buffer::release */
536 gst_buffer_unref (buf);
537 g_object_unref (alloc);
538 }
539
540 void
gst_wl_window_set_render_rectangle(GstWlWindow * window,gint x,gint y,gint w,gint h)541 gst_wl_window_set_render_rectangle (GstWlWindow * window, gint x, gint y,
542 gint w, gint h)
543 {
544 g_return_if_fail (window != NULL);
545
546 if (window->render_rectangle.x == x && window->render_rectangle.y == y &&
547 window->render_rectangle.w == w && window->render_rectangle.h == h)
548 return;
549
550 window->render_rectangle.x = x;
551 window->render_rectangle.y = y;
552 window->render_rectangle.w = w;
553 window->render_rectangle.h = h;
554
555 /* position the area inside the parent - needs a parent commit to apply */
556 if (window->area_subsurface)
557 wl_subsurface_set_position (window->area_subsurface, x, y);
558
559 if (window->is_area_surface_mapped)
560 gst_wl_window_update_borders (window);
561
562 if (!window->configured)
563 return;
564
565 if (window->video_width != 0) {
566 wl_subsurface_set_sync (window->video_subsurface);
567 gst_wl_window_resize_video_surface (window, TRUE);
568 }
569
570 wl_surface_commit (window->area_surface_wrapper);
571
572 if (window->video_width != 0)
573 wl_subsurface_set_desync (window->video_subsurface);
574 }
575