waylandsink: Commit the parent after creating subsurface
[platform/upstream/gstreamer.git] / 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
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);
198
199   window->area_surface = wl_compositor_create_surface (display->compositor);
200   window->video_surface = wl_compositor_create_surface (display->compositor);
201
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);
205
206   wl_proxy_set_queue ((struct wl_proxy *) window->area_surface_wrapper,
207       display->queue);
208   wl_proxy_set_queue ((struct wl_proxy *) window->video_surface_wrapper,
209       display->queue);
210
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);
216
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);
222   }
223
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);
227
228   return window;
229 }
230
231 void
232 gst_wl_window_ensure_fullscreen (GstWlWindow * window, gboolean fullscreen)
233 {
234   if (!window)
235     return;
236
237   if (window->display->xdg_wm_base) {
238     if (fullscreen)
239       xdg_toplevel_set_fullscreen (window->xdg_toplevel, NULL);
240     else
241       xdg_toplevel_unset_fullscreen (window->xdg_toplevel);
242   } else {
243     if (fullscreen)
244       wl_shell_surface_set_fullscreen (window->wl_shell_surface,
245           WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE, 0, NULL);
246     else
247       wl_shell_surface_set_toplevel (window->wl_shell_surface);
248   }
249 }
250
251 GstWlWindow *
252 gst_wl_window_new_toplevel (GstWlDisplay * display, const GstVideoInfo * info,
253     gboolean fullscreen, GMutex * render_lock)
254 {
255   GstWlWindow *window;
256   gint width;
257
258   window = gst_wl_window_new_internal (display, render_lock);
259
260   /* Check which protocol we will use (in order of preference) */
261   if (display->xdg_wm_base) {
262     gint64 timeout;
263
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");
269       goto error;
270     }
271     xdg_surface_add_listener (window->xdg_surface, &xdg_surface_listener,
272         window);
273
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");
278       goto error;
279     }
280     xdg_toplevel_add_listener (window->xdg_toplevel,
281         &xdg_toplevel_listener, window);
282
283     gst_wl_window_ensure_fullscreen (window, fullscreen);
284
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);
289
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,
294               timeout)) {
295         GST_WARNING ("The compositor did not send configure event.");
296         break;
297       }
298     }
299     g_mutex_unlock (&window->configure_mutex);
300   } else if (display->wl_shell) {
301     /* go toplevel */
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");
306       goto error;
307     }
308
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,
315         NULL);
316   } else {
317     GST_ERROR ("Unable to use either wl_shell, xdg_wm_base or "
318         "zwp_fullscreen_shell.");
319     goto error;
320   }
321
322   /* set the initial size to be the same as the reported video size */
323   width =
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);
326
327   return window;
328
329 error:
330   g_object_unref (window);
331   return NULL;
332 }
333
334 GstWlWindow *
335 gst_wl_window_new_in_surface (GstWlDisplay * display,
336     struct wl_surface * parent, GMutex * render_lock)
337 {
338   GstWlWindow *window;
339   window = gst_wl_window_new_internal (display, render_lock);
340
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);
346
347   wl_surface_commit (parent);
348
349   return window;
350 }
351
352 GstWlDisplay *
353 gst_wl_window_get_display (GstWlWindow * window)
354 {
355   g_return_val_if_fail (window != NULL, NULL);
356
357   return g_object_ref (window->display);
358 }
359
360 struct wl_surface *
361 gst_wl_window_get_wl_surface (GstWlWindow * window)
362 {
363   g_return_val_if_fail (window != NULL, NULL);
364
365   return window->video_surface_wrapper;
366 }
367
368 gboolean
369 gst_wl_window_is_toplevel (GstWlWindow * window)
370 {
371   g_return_val_if_fail (window != NULL, FALSE);
372
373   if (window->display->xdg_wm_base)
374     return (window->xdg_toplevel != NULL);
375   else
376     return (window->wl_shell_surface != NULL);
377 }
378
379 static void
380 gst_wl_window_resize_video_surface (GstWlWindow * window, gboolean commit)
381 {
382   GstVideoRectangle src = { 0, };
383   GstVideoRectangle dst = { 0, };
384   GstVideoRectangle res;
385
386   /* center the video_subsurface inside area_subsurface */
387   src.w = window->video_width;
388   src.h = window->video_height;
389   dst.w = window->render_rectangle.w;
390   dst.h = window->render_rectangle.h;
391
392   if (window->video_viewport) {
393     gst_video_sink_center_rect (src, dst, &res, TRUE);
394     wp_viewport_set_destination (window->video_viewport, res.w, res.h);
395   } else {
396     gst_video_sink_center_rect (src, dst, &res, FALSE);
397   }
398
399   wl_subsurface_set_position (window->video_subsurface, res.x, res.y);
400
401   if (commit) {
402     wl_surface_damage (window->video_surface_wrapper, 0, 0, res.w, res.h);
403     wl_surface_commit (window->video_surface_wrapper);
404   }
405
406   if (gst_wl_window_is_toplevel (window)) {
407     struct wl_region *region;
408
409     region = wl_compositor_create_region (window->display->compositor);
410     wl_region_add (region, 0, 0, window->render_rectangle.w,
411         window->render_rectangle.h);
412     wl_surface_set_input_region (window->area_surface, region);
413     wl_region_destroy (region);
414   }
415
416   /* this is saved for use in wl_surface_damage */
417   window->video_rectangle = res;
418 }
419
420 static void
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, window->render_rectangle.w,
428       window->render_rectangle.h);
429   wl_surface_set_opaque_region (window->area_surface, region);
430   wl_region_destroy (region);
431
432   if (!GST_VIDEO_INFO_HAS_ALPHA (info)) {
433     /* Set video opaque */
434     region = wl_compositor_create_region (window->display->compositor);
435     wl_region_add (region, 0, 0, window->render_rectangle.w,
436         window->render_rectangle.h);
437     wl_surface_set_opaque_region (window->video_surface, region);
438     wl_region_destroy (region);
439   }
440 }
441
442 void
443 gst_wl_window_render (GstWlWindow * window, GstWlBuffer * buffer,
444     const GstVideoInfo * info)
445 {
446   if (G_UNLIKELY (info)) {
447     window->video_width =
448         gst_util_uint64_scale_int_round (info->width, info->par_n, info->par_d);
449     window->video_height = info->height;
450
451     wl_subsurface_set_sync (window->video_subsurface);
452     gst_wl_window_resize_video_surface (window, FALSE);
453     gst_wl_window_set_opaque (window, info);
454   }
455
456   if (G_LIKELY (buffer))
457     gst_wl_buffer_attach (buffer, window->video_surface_wrapper);
458   else
459     wl_surface_attach (window->video_surface_wrapper, NULL, 0, 0);
460
461   wl_surface_damage (window->video_surface_wrapper, 0, 0,
462       window->video_rectangle.w, window->video_rectangle.h);
463   wl_surface_commit (window->video_surface_wrapper);
464
465   if (G_UNLIKELY (info)) {
466     /* commit also the parent (area_surface) in order to change
467      * the position of the video_subsurface */
468     wl_surface_damage (window->area_surface_wrapper, 0, 0,
469         window->render_rectangle.w, window->render_rectangle.h);
470     wl_surface_commit (window->area_surface_wrapper);
471     wl_subsurface_set_desync (window->video_subsurface);
472   }
473
474   wl_display_flush (window->display->display);
475 }
476
477 /* Update the buffer used to draw black borders. When we have viewporter
478  * support, this is a scaled up 1x1 image, and without we need an black image
479  * the size of the rendering areay. */
480 static void
481 gst_wl_window_update_borders (GstWlWindow * window)
482 {
483   GstVideoFormat format;
484   GstVideoInfo info;
485   gint width, height;
486   GstBuffer *buf;
487   struct wl_buffer *wlbuf;
488   GstWlBuffer *gwlbuf;
489   GstAllocator *alloc;
490
491   if (window->no_border_update)
492     return;
493
494   if (window->display->viewporter) {
495     width = height = 1;
496     window->no_border_update = TRUE;
497   } else {
498     width = window->render_rectangle.w;
499     height = window->render_rectangle.h;
500   }
501
502   /* we want WL_SHM_FORMAT_XRGB8888 */
503 #if G_BYTE_ORDER == G_BIG_ENDIAN
504   format = GST_VIDEO_FORMAT_xRGB;
505 #else
506   format = GST_VIDEO_FORMAT_BGRx;
507 #endif
508
509   /* draw the area_subsurface */
510   gst_video_info_set_format (&info, format, width, height);
511
512   alloc = gst_wl_shm_allocator_get ();
513
514   buf = gst_buffer_new_allocate (alloc, info.size, NULL);
515   gst_buffer_memset (buf, 0, 0, info.size);
516   wlbuf =
517       gst_wl_shm_memory_construct_wl_buffer (gst_buffer_peek_memory (buf, 0),
518       window->display, &info);
519   gwlbuf = gst_buffer_add_wl_buffer (buf, wlbuf, window->display);
520   gst_wl_buffer_attach (gwlbuf, window->area_surface_wrapper);
521
522   /* at this point, the GstWlBuffer keeps the buffer
523    * alive and will free it on wl_buffer::release */
524   gst_buffer_unref (buf);
525   g_object_unref (alloc);
526 }
527
528 void
529 gst_wl_window_set_render_rectangle (GstWlWindow * window, gint x, gint y,
530     gint w, gint h)
531 {
532   g_return_if_fail (window != NULL);
533
534   window->render_rectangle.x = x;
535   window->render_rectangle.y = y;
536   window->render_rectangle.w = w;
537   window->render_rectangle.h = h;
538
539   /* position the area inside the parent - needs a parent commit to apply */
540   if (window->area_subsurface)
541     wl_subsurface_set_position (window->area_subsurface, x, y);
542
543   /* change the size of the area */
544   if (window->area_viewport)
545     wp_viewport_set_destination (window->area_viewport, w, h);
546
547   gst_wl_window_update_borders (window);
548
549   if (window->video_width != 0) {
550     wl_subsurface_set_sync (window->video_subsurface);
551     gst_wl_window_resize_video_surface (window, TRUE);
552   }
553
554   wl_surface_damage (window->area_surface_wrapper, 0, 0, w, h);
555   wl_surface_commit (window->area_surface_wrapper);
556
557   if (window->video_width != 0)
558     wl_subsurface_set_desync (window->video_subsurface);
559 }