gst-plugins-base: gl: wayland: cleanup on close
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-base / gst-libs / gst / gl / wayland / gstglwindow_wayland_egl.c
1 /*
2  * GStreamer
3  * Copyright (C) 2008 Julien Isorce <julien.isorce@gmail.com>
4  * Copyright (C) 2012 Matthew Waters <ystreet00@gmail.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #define GLIB_DISABLE_DEPRECATION_WARNINGS
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <locale.h>
29 #include <sys/mman.h>
30 #include <unistd.h>
31
32 #include "wayland_event_source.h"
33
34 #include "../gstgl_fwd.h"
35 #include <gst/gl/gstglcontext.h>
36
37 #include "gstgldisplay_wayland.h"
38 #include "gstglwindow_wayland_egl.h"
39
40 #include "../gstglwindow_private.h"
41
42 const gchar *WlEGLErrorString ();
43
44 #define GST_CAT_DEFAULT gst_gl_window_debug
45
46 #define gst_gl_window_wayland_egl_parent_class parent_class
47 G_DEFINE_TYPE (GstGLWindowWaylandEGL, gst_gl_window_wayland_egl,
48     GST_TYPE_GL_WINDOW);
49
50 static guintptr gst_gl_window_wayland_egl_get_window_handle (GstGLWindow *
51     window);
52 static void gst_gl_window_wayland_egl_set_window_handle (GstGLWindow * window,
53     guintptr handle);
54 static void gst_gl_window_wayland_egl_show (GstGLWindow * window);
55 static void gst_gl_window_wayland_egl_draw (GstGLWindow * window);
56 static void gst_gl_window_wayland_egl_close (GstGLWindow * window);
57 static gboolean gst_gl_window_wayland_egl_open (GstGLWindow * window,
58     GError ** error);
59 static guintptr gst_gl_window_wayland_egl_get_display (GstGLWindow * window);
60 static gboolean gst_gl_window_wayland_egl_set_render_rectangle (GstGLWindow *
61     window, gint x, gint y, gint width, gint height);
62 static void gst_gl_window_wayland_egl_set_preferred_size (GstGLWindow * window,
63     gint width, gint height);
64
65 static void
66 pointer_handle_enter (void *data, struct wl_pointer *pointer, uint32_t serial,
67     struct wl_surface *surface, wl_fixed_t sx_w, wl_fixed_t sy_w)
68 {
69   GstGLWindowWaylandEGL *window_egl = data;
70   struct wl_buffer *buffer;
71   struct wl_cursor_image *image = NULL;
72
73   window_egl->display.serial = serial;
74
75   /* FIXME: Not sure how useful this is */
76   if (window_egl->display.default_cursor) {
77     image = window_egl->display.default_cursor->images[0];
78     buffer = wl_cursor_image_get_buffer (image);
79     wl_pointer_set_cursor (pointer, serial,
80         window_egl->display.cursor_surface, image->hotspot_x, image->hotspot_y);
81     wl_surface_attach (window_egl->display.cursor_surface, buffer, 0, 0);
82     wl_surface_damage (window_egl->display.cursor_surface, 0, 0,
83         image->width, image->height);
84     wl_surface_commit (window_egl->display.cursor_surface);
85   }
86 }
87
88 static void
89 pointer_handle_leave (void *data, struct wl_pointer *pointer, uint32_t serial,
90     struct wl_surface *surface)
91 {
92   GstGLWindowWaylandEGL *window_egl = data;
93   window_egl->display.serial = serial;
94 }
95
96 static void
97 pointer_handle_motion (void *data, struct wl_pointer *pointer, uint32_t time,
98     wl_fixed_t sx_w, wl_fixed_t sy_w)
99 {
100   GstGLWindowWaylandEGL *window_egl = data;
101   GstGLWindow *window = GST_GL_WINDOW (window_egl);
102
103   window_egl->display.pointer_x = wl_fixed_to_double (sx_w);
104   window_egl->display.pointer_y = wl_fixed_to_double (sy_w);
105   gst_gl_window_send_mouse_event (window, "mouse-move", 0,
106       window_egl->display.pointer_x, window_egl->display.pointer_y);
107 }
108
109 static void
110 pointer_handle_button (void *data, struct wl_pointer *pointer, uint32_t serial,
111     uint32_t time, uint32_t button, uint32_t state)
112 {
113   GstGLWindowWaylandEGL *window_egl = data;
114   GstGLWindow *window = GST_GL_WINDOW (window_egl);
115   const char *event_type;
116
117   event_type = state == 1 ? "mouse-button-press" : "mouse-button-release";
118   gst_gl_window_send_mouse_event (window, event_type, button,
119       window_egl->display.pointer_x, window_egl->display.pointer_y);
120 }
121
122 static void
123 pointer_handle_axis (void *data, struct wl_pointer *pointer, uint32_t time,
124     uint32_t axis, wl_fixed_t value)
125 {
126   GstGLWindowWaylandEGL *window_egl = data;
127   GstGLWindow *window = GST_GL_WINDOW (window_egl);
128   gdouble delta_x, delta_y;
129   gdouble delta = -wl_fixed_to_double (value);
130   if (axis == 1) {
131     delta_x = delta;
132     delta_y = 0;
133   } else {
134     delta_x = 0;
135     delta_y = delta;
136   }
137   gst_gl_window_send_scroll_event (window, window_egl->display.pointer_x,
138       window_egl->display.pointer_y, delta_x, delta_y);
139 }
140
141 static const struct wl_pointer_listener pointer_listener = {
142   pointer_handle_enter,
143   pointer_handle_leave,
144   pointer_handle_motion,
145   pointer_handle_button,
146   pointer_handle_axis,
147 };
148
149 static void
150 seat_handle_capabilities (void *data, struct wl_seat *seat,
151     enum wl_seat_capability caps)
152 {
153   GstGLWindowWaylandEGL *window_egl = data;
154   struct display *display = &window_egl->display;
155
156   if ((caps & WL_SEAT_CAPABILITY_POINTER) && !display->pointer) {
157     display->pointer = wl_seat_get_pointer (seat);
158     wl_pointer_set_user_data (display->pointer, window_egl);
159     wl_pointer_add_listener (display->pointer, &pointer_listener, window_egl);
160   } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && display->pointer) {
161     wl_pointer_destroy (display->pointer);
162     display->pointer = NULL;
163   }
164 #if 0
165   if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
166     input->keyboard = wl_seat_get_keyboard (seat);
167     wl_keyboard_set_user_data (input->keyboard, input);
168     wl_keyboard_add_listener (input->keyboard, &keyboard_listener, input);
169   } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
170     wl_keyboard_destroy (input->keyboard);
171     input->keyboard = NULL;
172   }
173 #endif
174 }
175
176 static void
177 seat_name (void *data, struct wl_seat *seat, const char *name)
178 {
179   GstGLWindowWaylandEGL *window_egl = data;
180
181   GST_TRACE_OBJECT (window_egl, "seat %p has name %s", seat, name);
182 }
183
184 static const struct wl_seat_listener seat_listener = {
185   seat_handle_capabilities,
186   seat_name
187 };
188
189 static void
190 handle_ping (void *data, struct wl_shell_surface *wl_shell_surface,
191     uint32_t serial)
192 {
193   GstGLWindowWaylandEGL *window_egl = data;
194
195   GST_TRACE_OBJECT (window_egl, "ping received serial %u", serial);
196
197   wl_shell_surface_pong (wl_shell_surface, serial);
198 }
199
200 static void window_resize (GstGLWindowWaylandEGL * window_egl, guint width,
201     guint height);
202
203 static void
204 handle_configure (void *data, struct wl_shell_surface *wl_shell_surface,
205     uint32_t edges, int32_t width, int32_t height)
206 {
207   GstGLWindowWaylandEGL *window_egl = data;
208
209   GST_DEBUG ("configure event on surface %p, %ix%i", wl_shell_surface, width,
210       height);
211
212   window_resize (window_egl, width, height);
213 }
214
215 static void
216 handle_popup_done (void *data, struct wl_shell_surface *wl_shell_surface)
217 {
218 }
219
220 static const struct wl_shell_surface_listener wl_shell_surface_listener = {
221   handle_ping,
222   handle_configure,
223   handle_popup_done
224 };
225
226 static void
227 handle_xdg_toplevel_close (void *data, struct xdg_toplevel *xdg_toplevel)
228 {
229   GstGLWindow *window = data;
230
231   GST_DEBUG ("XDG toplevel got a \"close\" event.");
232
233   if (window->close)
234     window->close (window->close_data);
235
236   gst_gl_display_remove_window (window->display, window);
237 }
238
239 static void
240 handle_xdg_toplevel_configure (void *data, struct xdg_toplevel *xdg_toplevel,
241     int32_t width, int32_t height, struct wl_array *states)
242 {
243   GstGLWindowWaylandEGL *window_egl = data;
244   const uint32_t *state;
245
246   GST_DEBUG ("configure event on XDG toplevel %p, %ix%i", xdg_toplevel,
247       width, height);
248
249   wl_array_for_each (state, states) {
250     switch (*state) {
251       case XDG_TOPLEVEL_STATE_FULLSCREEN:
252         window_egl->window.fullscreen = TRUE;
253         break;
254       case XDG_TOPLEVEL_STATE_MAXIMIZED:
255       case XDG_TOPLEVEL_STATE_RESIZING:
256       case XDG_TOPLEVEL_STATE_ACTIVATED:
257         break;
258     }
259   }
260
261   if (width > 0 && height > 0)
262     window_resize (window_egl, width, height);
263 }
264
265 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
266   handle_xdg_toplevel_configure,
267   handle_xdg_toplevel_close,
268 };
269
270 static void
271 handle_xdg_surface_configure (void *data, struct xdg_surface *xdg_surface,
272     uint32_t serial)
273 {
274   xdg_surface_ack_configure (xdg_surface, serial);
275 }
276
277 static const struct xdg_surface_listener xdg_surface_listener = {
278   handle_xdg_surface_configure,
279 };
280
281 static void
282 destroy_surfaces (GstGLWindowWaylandEGL * window_egl)
283 {
284   g_clear_pointer (&window_egl->window.subsurface, wl_subsurface_destroy);
285   g_clear_pointer (&window_egl->window.xdg_toplevel, xdg_toplevel_destroy);
286   g_clear_pointer (&window_egl->window.xdg_surface, xdg_surface_destroy);
287   g_clear_pointer (&window_egl->window.wl_shell_surface,
288       wl_shell_surface_destroy);
289   g_clear_pointer (&window_egl->window.surface, wl_surface_destroy);
290   g_clear_pointer (&window_egl->window.native, wl_egl_window_destroy);
291 }
292
293 static void
294 create_xdg_surface_and_toplevel (GstGLWindowWaylandEGL * window_egl)
295 {
296   struct xdg_surface *xdg_surface;
297   struct xdg_toplevel *xdg_toplevel;
298
299   GST_DEBUG ("Creating surfaces XDG-shell");
300
301   /* First create the XDG surface */
302   xdg_surface = xdg_wm_base_get_xdg_surface (window_egl->display.xdg_wm_base,
303       window_egl->window.surface);
304   xdg_surface_add_listener (xdg_surface, &xdg_surface_listener, window_egl);
305
306   /* Then the XDG top-level */
307   xdg_toplevel = xdg_surface_get_toplevel (xdg_surface);
308   xdg_toplevel_set_title (xdg_toplevel, "OpenGL Renderer");
309   xdg_toplevel_add_listener (xdg_toplevel, &xdg_toplevel_listener, window_egl);
310
311   /* Commit the xdg_surface state */
312   wl_surface_commit (window_egl->window.surface);
313
314   /* And save them into the fields */
315   window_egl->window.xdg_surface = xdg_surface;
316   window_egl->window.xdg_toplevel = xdg_toplevel;
317 }
318
319 static void
320 create_wl_shell_surface (GstGLWindowWaylandEGL * window_egl)
321 {
322   struct wl_shell_surface *wl_shell_surface;
323
324   GST_DEBUG ("Creating surfaces for wl-shell");
325
326   wl_shell_surface = wl_shell_get_shell_surface (window_egl->display.shell,
327       window_egl->window.surface);
328
329   wl_shell_surface_add_listener (wl_shell_surface, &wl_shell_surface_listener,
330       window_egl);
331   wl_shell_surface_set_title (wl_shell_surface, "OpenGL Renderer");
332   wl_shell_surface_set_toplevel (wl_shell_surface);
333
334   window_egl->window.wl_shell_surface = wl_shell_surface;
335 }
336
337 static void
338 create_surfaces (GstGLWindowWaylandEGL * window_egl)
339 {
340   gint width, height;
341
342   if (!window_egl->window.surface) {
343     window_egl->window.surface =
344         wl_compositor_create_surface (window_egl->display.compositor);
345   }
346
347   if (window_egl->window.foreign_surface) {
348     /* (re)parent */
349     if (!window_egl->display.subcompositor) {
350       GST_ERROR_OBJECT (window_egl,
351           "Wayland server does not support subsurfaces");
352       window_egl->window.foreign_surface = NULL;
353       goto shell_window;
354     }
355
356     if (!window_egl->window.subsurface) {
357       window_egl->window.subsurface =
358           wl_subcompositor_get_subsurface (window_egl->display.subcompositor,
359           window_egl->window.surface, window_egl->window.foreign_surface);
360
361       wl_subsurface_set_position (window_egl->window.subsurface,
362           window_egl->window.window_x, window_egl->window.window_y);
363       wl_subsurface_set_desync (window_egl->window.subsurface);
364     }
365   } else {
366   shell_window:
367     if (window_egl->display.xdg_wm_base) {
368       if (!window_egl->window.xdg_surface)
369         create_xdg_surface_and_toplevel (window_egl);
370     } else if (!window_egl->window.wl_shell_surface) {
371       create_wl_shell_surface (window_egl);
372     }
373   }
374
375   /*
376    * render_rect is the application requested size so choose that first if
377    * available.
378    * Else choose the already chosen size if set
379    * Else choose the preferred size if set
380    * Else choose a default value
381    */
382   if (window_egl->window.render_rect.w > 0)
383     width = window_egl->window.render_rect.w;
384   else if (window_egl->window.window_width > 0)
385     width = window_egl->window.window_width;
386   else if (window_egl->window.preferred_width > 0)
387     width = window_egl->window.preferred_width;
388   else
389     width = 320;
390   window_egl->window.window_width = width;
391
392   if (window_egl->window.render_rect.h > 0)
393     height = window_egl->window.render_rect.h;
394   else if (window_egl->window.window_height > 0)
395     height = window_egl->window.window_height;
396   else if (window_egl->window.preferred_height > 0)
397     height = window_egl->window.preferred_height;
398   else
399     height = 240;
400   window_egl->window.window_height = height;
401
402   if (!window_egl->window.native) {
403     gst_gl_window_resize (GST_GL_WINDOW (window_egl), width, height);
404
405     window_egl->window.native =
406         wl_egl_window_create (window_egl->window.surface, width, height);
407   }
408 }
409
410 static void
411 gst_gl_window_wayland_egl_class_init (GstGLWindowWaylandEGLClass * klass)
412 {
413   GstGLWindowClass *window_class = (GstGLWindowClass *) klass;
414
415   window_class->get_window_handle =
416       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_get_window_handle);
417   window_class->set_window_handle =
418       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_window_handle);
419   window_class->show = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_show);
420   window_class->draw = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_draw);
421   window_class->close = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_close);
422   window_class->open = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_open);
423   window_class->get_display =
424       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_get_display);
425   window_class->set_render_rectangle =
426       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_render_rectangle);
427   window_class->set_preferred_size =
428       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_preferred_size);
429 }
430
431 static void
432 gst_gl_window_wayland_egl_init (GstGLWindowWaylandEGL * window)
433 {
434   window->window.render_rect.w = window->window.render_rect.h = -1;
435 }
436
437 /* Must be called in the gl thread */
438 GstGLWindowWaylandEGL *
439 gst_gl_window_wayland_egl_new (GstGLDisplay * display)
440 {
441   GstGLWindowWaylandEGL *window;
442
443   if ((gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_WAYLAND)
444       == 0)
445     /* we require a wayland display to create wayland surfaces */
446     return NULL;
447
448   GST_DEBUG ("creating Wayland EGL window");
449
450   window = g_object_new (GST_TYPE_GL_WINDOW_WAYLAND_EGL, NULL);
451   gst_object_ref_sink (window);
452
453   return window;
454 }
455
456 static void
457 gst_gl_window_wayland_egl_close (GstGLWindow * gl_window)
458 {
459   GstGLWindowWaylandEGL *window_egl;
460   struct display *display;
461
462   window_egl = GST_GL_WINDOW_WAYLAND_EGL (gl_window);
463   display = &window_egl->display;
464
465   if (display->pointer != NULL) {
466     wl_pointer_destroy (display->pointer);
467     display->pointer = NULL;
468   }
469
470   destroy_surfaces (window_egl);
471
472   /* As we are about to destroy the wl_source, we need to ensure everything
473    * has been sent synchronously, otherwise we will be leaking surfaces on
474    * server, leaving the window visible and unrefreshed on screen. */
475   wl_display_flush (GST_GL_DISPLAY_WAYLAND (gl_window->display)->display);
476
477   g_source_destroy (window_egl->wl_source);
478   g_source_unref (window_egl->wl_source);
479   window_egl->wl_source = NULL;
480
481   wl_proxy_wrapper_destroy (window_egl->display.display);
482   wl_event_queue_destroy (window_egl->window.queue);
483
484   GST_GL_WINDOW_CLASS (parent_class)->close (gl_window);
485 }
486
487 static void
488 handle_xdg_wm_base_ping (void *user_data, struct xdg_wm_base *xdg_wm_base,
489     uint32_t serial)
490 {
491   xdg_wm_base_pong (xdg_wm_base, serial);
492 }
493
494 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
495   handle_xdg_wm_base_ping
496 };
497
498 static void
499 registry_handle_global (void *data, struct wl_registry *registry,
500     uint32_t name, const char *interface, uint32_t version)
501 {
502   GstGLWindowWaylandEGL *window_wayland = data;
503
504   GST_TRACE_OBJECT (window_wayland, "registry_handle_global with registry %p, "
505       "name %" G_GUINT32_FORMAT ", interface %s, version %u", registry, name,
506       interface, version);
507
508   if (g_strcmp0 (interface, "wl_compositor") == 0) {
509     window_wayland->display.compositor =
510         wl_registry_bind (registry, name, &wl_compositor_interface, 1);
511   } else if (g_strcmp0 (interface, "wl_subcompositor") == 0) {
512     window_wayland->display.subcompositor =
513         wl_registry_bind (registry, name, &wl_subcompositor_interface, 1);
514   } else if (g_strcmp0 (interface, "xdg_wm_base") == 0) {
515     window_wayland->display.xdg_wm_base =
516         wl_registry_bind (registry, name, &xdg_wm_base_interface, 1);
517     xdg_wm_base_add_listener (window_wayland->display.xdg_wm_base,
518         &xdg_wm_base_listener, window_wayland);
519   } else if (g_strcmp0 (interface, "wl_shell") == 0) {
520     window_wayland->display.shell =
521         wl_registry_bind (registry, name, &wl_shell_interface, 1);
522   } else if (g_strcmp0 (interface, "wl_seat") == 0) {
523     window_wayland->display.seat =
524         wl_registry_bind (registry, name, &wl_seat_interface, 4);
525     wl_seat_add_listener (window_wayland->display.seat, &seat_listener,
526         window_wayland);
527   }
528 }
529
530 static void
531 registry_handle_global_remove (void *data, struct wl_registry *registry,
532     uint32_t name)
533 {
534   GstGLWindowWaylandEGL *window_wayland = data;
535
536   /* TODO: deal with any registry objects that may be removed */
537   GST_TRACE_OBJECT (window_wayland, "wl_registry %p global_remove %"
538       G_GUINT32_FORMAT, registry, name);
539 }
540
541 static const struct wl_registry_listener registry_listener = {
542   registry_handle_global,
543   registry_handle_global_remove,
544 };
545
546 static gboolean
547 gst_gl_window_wayland_egl_open (GstGLWindow * window, GError ** error)
548 {
549   GstGLDisplayWayland *display;
550   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
551
552   if (!GST_IS_GL_DISPLAY_WAYLAND (window->display)) {
553     g_set_error (error, GST_GL_WINDOW_ERROR,
554         GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
555         "Failed to retrieve Wayland display (wrong type?)");
556     return FALSE;
557   }
558   display = GST_GL_DISPLAY_WAYLAND (window->display);
559
560   if (!display->display) {
561     g_set_error (error, GST_GL_WINDOW_ERROR,
562         GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
563         "Failed to retrieve Wayland display");
564     return FALSE;
565   }
566
567   /* we create a proxy wrapper for the display so that we can set the queue on
568    * it. This allows us to avoid sprinkling `wl_proxy_set_queue()` calls for
569    * each wayland resource we create as well as removing a race between
570    * creation and the `wl_proxy_set_queue()` call. */
571   window_egl->display.display = wl_proxy_create_wrapper (display->display);
572   window_egl->window.queue = wl_display_create_queue (display->display);
573
574   wl_proxy_set_queue ((struct wl_proxy *) window_egl->display.display,
575       window_egl->window.queue);
576
577   window_egl->display.registry =
578       wl_display_get_registry (window_egl->display.display);
579   wl_registry_add_listener (window_egl->display.registry, &registry_listener,
580       window_egl);
581
582   if (wl_display_roundtrip_queue (display->display,
583           window_egl->window.queue) < 0) {
584     g_set_error (error, GST_GL_WINDOW_ERROR,
585         GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
586         "Failed to perform a wayland roundtrip");
587     return FALSE;
588   }
589
590   window_egl->wl_source = wayland_event_source_new (display->display,
591       window_egl->window.queue);
592
593   if (!GST_GL_WINDOW_CLASS (parent_class)->open (window, error))
594     return FALSE;
595
596   g_source_attach (window_egl->wl_source, window->main_context);
597
598   return TRUE;
599 }
600
601 void
602 gst_gl_window_wayland_egl_create_window (GstGLWindowWaylandEGL * window_egl)
603 {
604   create_surfaces (window_egl);
605 }
606
607 static guintptr
608 gst_gl_window_wayland_egl_get_window_handle (GstGLWindow * window)
609 {
610   return (guintptr) GST_GL_WINDOW_WAYLAND_EGL (window)->window.native;
611 }
612
613 static void
614 gst_gl_window_wayland_egl_set_window_handle (GstGLWindow * window,
615     guintptr handle)
616 {
617   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
618   struct wl_surface *surface = (struct wl_surface *) handle;
619
620   /* already set the NULL handle */
621   if (surface == NULL && window_egl->window.foreign_surface == NULL)
622     return;
623
624   /* unparent */
625   destroy_surfaces (window_egl);
626   window_egl->window.foreign_surface = surface;
627   create_surfaces (window_egl);
628 }
629
630 static void
631 _roundtrip_async (GstGLWindow * window)
632 {
633   GstGLDisplayWayland *display = GST_GL_DISPLAY_WAYLAND (window->display);
634   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
635
636   create_surfaces (window_egl);
637
638   if (wl_display_roundtrip_queue (display->display,
639           window_egl->window.queue) < 0)
640     GST_WARNING_OBJECT (window, "failed a roundtrip");
641 }
642
643 static void
644 gst_gl_window_wayland_egl_show (GstGLWindow * window)
645 {
646   gst_gl_window_send_message (window, (GstGLWindowCB) _roundtrip_async, window);
647 }
648
649 static void
650 window_resize (GstGLWindowWaylandEGL * window_egl, guint width, guint height)
651 {
652   GstGLWindow *window = GST_GL_WINDOW (window_egl);
653
654   GST_DEBUG ("resizing window from %ux%u to %ux%u",
655       window_egl->window.window_width, window_egl->window.window_height, width,
656       height);
657
658   if (window_egl->window.native) {
659     wl_egl_window_resize (window_egl->window.native, width, height, 0, 0);
660   }
661
662   gst_gl_window_resize (window, width, height);
663
664   window_egl->window.window_width = width;
665   window_egl->window.window_height = height;
666 }
667
668 static void
669 draw_cb (gpointer data)
670 {
671   GstGLWindowWaylandEGL *window_egl = data;
672   GstGLWindow *window = GST_GL_WINDOW (window_egl);
673   GstGLContext *context = gst_gl_window_get_context (window);
674
675   create_surfaces (window_egl);
676
677   if (window_egl->window.subsurface)
678     wl_subsurface_set_desync (window_egl->window.subsurface);
679
680   if (window->queue_resize) {
681     guint width, height;
682
683     gst_gl_window_get_surface_dimensions (window, &width, &height);
684     gst_gl_window_resize (window, width, height);
685   }
686
687   if (window->draw)
688     window->draw (window->draw_data);
689
690   gst_gl_context_swap_buffers (context);
691
692   if (window_egl->window.subsurface)
693     wl_subsurface_set_desync (window_egl->window.subsurface);
694
695   gst_object_unref (context);
696 }
697
698 static void
699 gst_gl_window_wayland_egl_draw (GstGLWindow * window)
700 {
701   gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, window);
702 }
703
704 struct SetRenderRectangle
705 {
706   GstGLWindowWaylandEGL *window_egl;
707   GstVideoRectangle rect;
708 };
709
710 static void
711 _free_set_render_rectangle (struct SetRenderRectangle *render)
712 {
713   if (render) {
714     if (render->window_egl)
715       gst_object_unref (render->window_egl);
716     g_free (render);
717   }
718 }
719
720 static void
721 _set_render_rectangle (gpointer data)
722 {
723   struct SetRenderRectangle *render = data;
724
725   GST_LOG_OBJECT (render->window_egl, "setting render rectangle %i,%i+%ix%i",
726       render->rect.x, render->rect.y, render->rect.w, render->rect.h);
727
728   if (render->window_egl->window.subsurface) {
729     wl_subsurface_set_sync (render->window_egl->window.subsurface);
730     wl_subsurface_set_position (render->window_egl->window.subsurface,
731         render->rect.x, render->rect.y);
732     render->window_egl->window.window_x = render->rect.x;
733     render->window_egl->window.window_y = render->rect.y;
734   }
735
736   window_resize (render->window_egl, render->rect.w, render->rect.h);
737
738   render->window_egl->window.render_rect = render->rect;
739 }
740
741 static gboolean
742 gst_gl_window_wayland_egl_set_render_rectangle (GstGLWindow * window,
743     gint x, gint y, gint width, gint height)
744 {
745   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
746   struct SetRenderRectangle *render;
747
748   render = g_new0 (struct SetRenderRectangle, 1);
749   render->window_egl = gst_object_ref (window_egl);
750   render->rect.x = x;
751   render->rect.y = y;
752   render->rect.w = width;
753   render->rect.h = height;
754
755   gst_gl_window_send_message_async (window,
756       (GstGLWindowCB) _set_render_rectangle, render,
757       (GDestroyNotify) _free_set_render_rectangle);
758
759   return TRUE;
760 }
761
762 static void
763 gst_gl_window_wayland_egl_set_preferred_size (GstGLWindow * window, gint width,
764     gint height)
765 {
766   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
767
768   window_egl->window.preferred_width = width;
769   window_egl->window.preferred_height = height;
770   if (window_egl->window.render_rect.w < 0
771       && window_egl->window.render_rect.h < 0) {
772     if (window_egl->window.window_height != height
773         || window_egl->window.window_width != width) {
774       window_resize (window_egl, width, height);
775     }
776   }
777 }
778
779 static guintptr
780 gst_gl_window_wayland_egl_get_display (GstGLWindow * window)
781 {
782   GstGLDisplayWayland *display = GST_GL_DISPLAY_WAYLAND (window->display);
783
784   return (guintptr) display->display;
785 }