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