gl/wayland: Don't prefix wl_shell struct field
[platform/upstream/gstreamer.git] / 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 <linux/input.h>
29
30 #include "wayland_event_source.h"
31
32 #include "../gstgl_fwd.h"
33 #include <gst/gl/gstglcontext.h>
34
35 #include "gstgldisplay_wayland.h"
36 #include "gstgldisplay_wayland_private.h"
37 #include "gstglwindow_wayland_egl.h"
38
39 #include "../gstglwindow_private.h"
40
41 const gchar *WlEGLErrorString ();
42
43 #define GST_CAT_DEFAULT gst_gl_window_debug
44
45 #define gst_gl_window_wayland_egl_parent_class parent_class
46 G_DEFINE_TYPE (GstGLWindowWaylandEGL, gst_gl_window_wayland_egl,
47     GST_TYPE_GL_WINDOW);
48
49 static guintptr gst_gl_window_wayland_egl_get_window_handle (GstGLWindow *
50     window);
51 static void gst_gl_window_wayland_egl_set_window_handle (GstGLWindow * window,
52     guintptr handle);
53 static void gst_gl_window_wayland_egl_show (GstGLWindow * window);
54 static void gst_gl_window_wayland_egl_draw (GstGLWindow * window);
55 static void gst_gl_window_wayland_egl_close (GstGLWindow * window);
56 static gboolean gst_gl_window_wayland_egl_open (GstGLWindow * window,
57     GError ** error);
58 static guintptr gst_gl_window_wayland_egl_get_display (GstGLWindow * window);
59 static gboolean gst_gl_window_wayland_egl_set_render_rectangle (GstGLWindow *
60     window, gint x, gint y, gint width, gint height);
61 static void gst_gl_window_wayland_egl_set_preferred_size (GstGLWindow * window,
62     gint width, gint height);
63
64 #if 0
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   if (window_egl->display.default_cursor) {
76     image = window_egl->display.default_cursor->images[0];
77     buffer = wl_cursor_image_get_buffer (image);
78     wl_pointer_set_cursor (pointer, serial,
79         window_egl->display.cursor_surface, image->hotspot_x, image->hotspot_y);
80     wl_surface_attach (window_egl->display.cursor_surface, buffer, 0, 0);
81     wl_surface_damage (window_egl->display.cursor_surface, 0, 0,
82         image->width, image->height);
83     wl_surface_commit (window_egl->display.cursor_surface);
84   }
85 }
86
87 static void
88 pointer_handle_leave (void *data, struct wl_pointer *pointer, uint32_t serial,
89     struct wl_surface *surface)
90 {
91   GstGLWindowWaylandEGL *window_egl = data;
92   window_egl->display.serial = serial;
93 }
94
95 static void
96 pointer_handle_motion (void *data, struct wl_pointer *pointer, uint32_t time,
97     wl_fixed_t sx_w, wl_fixed_t sy_w)
98 {
99   GstGLWindowWaylandEGL *window_egl = data;
100
101   window_egl->display.pointer_x = wl_fixed_to_double (sx_w);
102   window_egl->display.pointer_y = wl_fixed_to_double (sy_w);
103 }
104
105 enum wl_edges
106 {
107   WL_EDGE_NONE = 0,
108   WL_EDGE_TOP = 1,
109   WL_EDGE_BOTTOM = 2,
110   WL_EDGE_LEFT = 4,
111   WL_EDGE_RIGHT = 8,
112 };
113
114 static guint
115 _get_closest_pointer_corner (GstGLWindowWaylandEGL * window_egl)
116 {
117   guint edges = 0;
118   gdouble win_width, win_height;
119   gdouble p_x, p_y;
120
121   win_width = (gdouble) window_egl->window.window_width;
122   win_height = (gdouble) window_egl->window.window_height;
123   p_x = window_egl->display.pointer_x;
124   p_y = window_egl->display.pointer_y;
125
126   if (win_width == 0.0 || win_height == 0.0)
127     return WL_EDGE_NONE;
128
129   edges |= win_width / 2.0 - p_x < 0.0 ? WL_EDGE_RIGHT : WL_EDGE_LEFT;
130   edges |= win_height / 2.0 - p_y < 0.0 ? WL_EDGE_BOTTOM : WL_EDGE_TOP;
131
132   return edges;
133 }
134
135 static void
136 pointer_handle_button (void *data, struct wl_pointer *pointer, uint32_t serial,
137     uint32_t time, uint32_t button, uint32_t state_w)
138 {
139   GstGLWindowWaylandEGL *window_egl = data;
140   guint edges = _get_closest_pointer_corner (window_egl);
141   window_egl->display.serial = serial;
142
143   if (button == BTN_LEFT && state_w == WL_POINTER_BUTTON_STATE_PRESSED)
144     wl_shell_surface_move (window_egl->window.wl_shell_surface,
145         window_egl->display.seat, serial);
146
147   if (button == BTN_RIGHT && state_w == WL_POINTER_BUTTON_STATE_PRESSED)
148     wl_shell_surface_resize (window_egl->window.wl_shell_surface,
149         window_egl->display.seat, serial, edges);
150 }
151
152 static void
153 pointer_handle_axis (void *data, struct wl_pointer *pointer, uint32_t time,
154     uint32_t axis, wl_fixed_t value)
155 {
156 }
157
158 static const struct wl_pointer_listener pointer_listener = {
159   pointer_handle_enter,
160   pointer_handle_leave,
161   pointer_handle_motion,
162   pointer_handle_button,
163   pointer_handle_axis,
164 };
165
166 static void
167 seat_handle_capabilities (void *data, struct wl_seat *seat,
168     enum wl_seat_capability caps)
169 {
170   GstGLWindowWaylandEGL *window_egl = data;
171   struct display *display = &window_egl->display;
172
173   if ((caps & WL_SEAT_CAPABILITY_POINTER) && !display->pointer) {
174     display->pointer = wl_seat_get_pointer (seat);
175     wl_pointer_set_user_data (display->pointer, window_egl);
176     wl_pointer_add_listener (display->pointer, &pointer_listener, window_egl);
177   } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && display->pointer) {
178     wl_pointer_destroy (display->pointer);
179     display->pointer = NULL;
180   }
181 #if 0
182   if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
183     input->keyboard = wl_seat_get_keyboard (seat);
184     wl_keyboard_set_user_data (input->keyboard, input);
185     wl_keyboard_add_listener (input->keyboard, &keyboard_listener, input);
186   } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
187     wl_keyboard_destroy (input->keyboard);
188     input->keyboard = NULL;
189   }
190 #endif
191 }
192
193 static const struct wl_seat_listener seat_listener = {
194   seat_handle_capabilities,
195 };
196 #endif
197 static void
198 handle_ping (void *data, struct wl_shell_surface *wl_shell_surface,
199     uint32_t serial)
200 {
201   GstGLWindowWaylandEGL *window_egl = data;
202
203   GST_TRACE_OBJECT (window_egl, "ping received serial %u", serial);
204
205   wl_shell_surface_pong (wl_shell_surface, serial);
206 }
207
208 static void window_resize (GstGLWindowWaylandEGL * window_egl, guint width,
209     guint height);
210
211 static void
212 handle_configure (void *data, struct wl_shell_surface *wl_shell_surface,
213     uint32_t edges, int32_t width, int32_t height)
214 {
215   GstGLWindowWaylandEGL *window_egl = data;
216
217   GST_DEBUG ("configure event on surface %p, %ix%i", wl_shell_surface, width,
218       height);
219
220   window_resize (window_egl, width, height);
221 }
222
223 static void
224 handle_popup_done (void *data, struct wl_shell_surface *wl_shell_surface)
225 {
226 }
227
228 static const struct wl_shell_surface_listener wl_shell_surface_listener = {
229   handle_ping,
230   handle_configure,
231   handle_popup_done
232 };
233
234 static void
235 handle_xdg_toplevel_close (void *data, struct xdg_toplevel *xdg_toplevel)
236 {
237   GstGLWindowWaylandEGL *window_egl = data;
238
239   GST_DEBUG ("XDG toplevel got a \"close\" event.");
240
241   gst_gl_window_wayland_egl_close (GST_GL_WINDOW (window_egl));
242 }
243
244 static void
245 handle_xdg_toplevel_configure (void *data, struct xdg_toplevel *xdg_toplevel,
246     int32_t width, int32_t height, struct wl_array *states)
247 {
248   GstGLWindowWaylandEGL *window_egl = data;
249   const uint32_t *state;
250
251   GST_DEBUG ("configure event on XDG toplevel %p, %ix%i", xdg_toplevel,
252       width, height);
253
254   wl_array_for_each (state, states) {
255     switch (*state) {
256       case XDG_TOPLEVEL_STATE_FULLSCREEN:
257         window_egl->window.fullscreen = TRUE;
258         break;
259       case XDG_TOPLEVEL_STATE_MAXIMIZED:
260       case XDG_TOPLEVEL_STATE_RESIZING:
261       case XDG_TOPLEVEL_STATE_ACTIVATED:
262         break;
263     }
264   }
265
266   if (width > 0 && height > 0)
267     window_resize (window_egl, width, height);
268 }
269
270 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
271   handle_xdg_toplevel_configure,
272   handle_xdg_toplevel_close,
273 };
274
275 static void
276 handle_xdg_surface_configure (void *data, struct xdg_surface *xdg_surface,
277     uint32_t serial)
278 {
279   xdg_surface_ack_configure (xdg_surface, serial);
280 }
281
282 static const struct xdg_surface_listener xdg_surface_listener = {
283   handle_xdg_surface_configure,
284 };
285
286 static void
287 destroy_surfaces (GstGLWindowWaylandEGL * window_egl)
288 {
289   g_clear_pointer (&window_egl->window.subsurface, wl_subsurface_destroy);
290   g_clear_pointer (&window_egl->window.xdg_toplevel, xdg_toplevel_destroy);
291   g_clear_pointer (&window_egl->window.xdg_surface, xdg_surface_destroy);
292   g_clear_pointer (&window_egl->window.wl_shell_surface,
293       wl_shell_surface_destroy);
294   g_clear_pointer (&window_egl->window.surface, wl_surface_destroy);
295   g_clear_pointer (&window_egl->window.native, wl_egl_window_destroy);
296 }
297
298 static void
299 create_xdg_surface_and_toplevel (GstGLWindowWaylandEGL * window_egl)
300 {
301   GstGLDisplayWayland *display =
302       GST_GL_DISPLAY_WAYLAND (GST_GL_WINDOW (window_egl)->display);
303   struct xdg_wm_base *xdg_wm_base;
304   struct xdg_surface *xdg_surface;
305   struct xdg_toplevel *xdg_toplevel;
306
307   GST_DEBUG ("Creating surfaces XDG-shell");
308
309   /* First create the XDG surface */
310   xdg_wm_base = gst_gl_display_wayland_get_xdg_wm_base (display);
311   xdg_surface = xdg_wm_base_get_xdg_surface (xdg_wm_base,
312       window_egl->window.surface);
313   if (window_egl->window.queue) {
314     wl_proxy_set_queue ((struct wl_proxy *) xdg_surface,
315         window_egl->window.queue);
316   }
317   xdg_surface_add_listener (xdg_surface, &xdg_surface_listener, window_egl);
318
319   /* Then the XDG top-level */
320   xdg_toplevel = xdg_surface_get_toplevel (xdg_surface);
321   xdg_toplevel_set_title (xdg_toplevel, "OpenGL Renderer");
322   if (window_egl->window.queue) {
323     wl_proxy_set_queue ((struct wl_proxy *) xdg_toplevel,
324         window_egl->window.queue);
325   }
326   xdg_toplevel_add_listener (xdg_toplevel, &xdg_toplevel_listener, window_egl);
327
328   /* Commit the xdg_surface state */
329   wl_surface_commit (window_egl->window.surface);
330
331   /* And save them into the fields */
332   window_egl->window.xdg_surface = xdg_surface;
333   window_egl->window.xdg_toplevel = xdg_toplevel;
334 }
335
336 static void
337 create_wl_shell_surface (GstGLWindowWaylandEGL * window_egl)
338 {
339   GstGLDisplayWayland *display =
340       GST_GL_DISPLAY_WAYLAND (GST_GL_WINDOW (window_egl)->display);
341   struct wl_shell_surface *wl_shell_surface;
342
343   GST_DEBUG ("Creating surfaces for wl-shell");
344
345   wl_shell_surface = wl_shell_get_shell_surface (display->shell,
346       window_egl->window.surface);
347
348   if (window_egl->window.queue) {
349     wl_proxy_set_queue ((struct wl_proxy *) wl_shell_surface,
350         window_egl->window.queue);
351   }
352
353   wl_shell_surface_add_listener (wl_shell_surface, &wl_shell_surface_listener,
354       window_egl);
355   wl_shell_surface_set_title (wl_shell_surface, "OpenGL Renderer");
356   wl_shell_surface_set_toplevel (wl_shell_surface);
357
358   window_egl->window.wl_shell_surface = wl_shell_surface;
359 }
360
361 static void
362 create_surfaces (GstGLWindowWaylandEGL * window_egl)
363 {
364   GstGLDisplayWayland *display =
365       GST_GL_DISPLAY_WAYLAND (GST_GL_WINDOW (window_egl)->display);
366   gint width, height;
367
368   if (!window_egl->window.surface) {
369     window_egl->window.surface =
370         wl_compositor_create_surface (display->compositor);
371     if (window_egl->window.queue)
372       wl_proxy_set_queue ((struct wl_proxy *) window_egl->window.surface,
373           window_egl->window.queue);
374   }
375
376   if (window_egl->window.foreign_surface) {
377     /* (re)parent */
378     if (!display->subcompositor) {
379       GST_ERROR_OBJECT (window_egl,
380           "Wayland server does not support subsurfaces");
381       window_egl->window.foreign_surface = NULL;
382       goto shell_window;
383     }
384
385     if (!window_egl->window.subsurface) {
386       window_egl->window.subsurface =
387           wl_subcompositor_get_subsurface (display->subcompositor,
388           window_egl->window.surface, window_egl->window.foreign_surface);
389       if (window_egl->window.queue)
390         wl_proxy_set_queue ((struct wl_proxy *) window_egl->window.subsurface,
391             window_egl->window.queue);
392
393       wl_subsurface_set_position (window_egl->window.subsurface,
394           window_egl->window.window_x, window_egl->window.window_y);
395       wl_subsurface_set_desync (window_egl->window.subsurface);
396     }
397   } else {
398   shell_window:
399     if (gst_gl_display_wayland_get_xdg_wm_base (display)) {
400       if (!window_egl->window.xdg_surface)
401         create_xdg_surface_and_toplevel (window_egl);
402     } else if (!window_egl->window.wl_shell_surface) {
403       create_wl_shell_surface (window_egl);
404     }
405   }
406
407   /*
408    * render_rect is the application requested size so choose that first if
409    * available.
410    * Else choose the already chosen size if set
411    * Else choose the preferred size if set
412    * Else choose a default value
413    */
414   if (window_egl->window.render_rect.w > 0)
415     width = window_egl->window.render_rect.w;
416   else if (window_egl->window.window_width > 0)
417     width = window_egl->window.window_width;
418   else if (window_egl->window.preferred_width > 0)
419     width = window_egl->window.preferred_width;
420   else
421     width = 320;
422   window_egl->window.window_width = width;
423
424   if (window_egl->window.render_rect.h > 0)
425     height = window_egl->window.render_rect.h;
426   else if (window_egl->window.window_height > 0)
427     height = window_egl->window.window_height;
428   else if (window_egl->window.preferred_height > 0)
429     height = window_egl->window.preferred_height;
430   else
431     height = 240;
432   window_egl->window.window_height = height;
433
434   if (!window_egl->window.native) {
435     gst_gl_window_resize (GST_GL_WINDOW (window_egl), width, height);
436
437     window_egl->window.native =
438         wl_egl_window_create (window_egl->window.surface, width, height);
439     if (window_egl->window.queue)
440       wl_proxy_set_queue ((struct wl_proxy *) window_egl->window.native,
441           window_egl->window.queue);
442   }
443 }
444
445 static void
446 gst_gl_window_wayland_egl_class_init (GstGLWindowWaylandEGLClass * klass)
447 {
448   GstGLWindowClass *window_class = (GstGLWindowClass *) klass;
449
450   window_class->get_window_handle =
451       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_get_window_handle);
452   window_class->set_window_handle =
453       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_window_handle);
454   window_class->show = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_show);
455   window_class->draw = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_draw);
456   window_class->close = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_close);
457   window_class->open = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_open);
458   window_class->get_display =
459       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_get_display);
460   window_class->set_render_rectangle =
461       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_render_rectangle);
462   window_class->set_preferred_size =
463       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_preferred_size);
464 }
465
466 static void
467 gst_gl_window_wayland_egl_init (GstGLWindowWaylandEGL * window)
468 {
469   window->window.render_rect.w = window->window.render_rect.h = -1;
470 }
471
472 /* Must be called in the gl thread */
473 GstGLWindowWaylandEGL *
474 gst_gl_window_wayland_egl_new (GstGLDisplay * display)
475 {
476   GstGLWindowWaylandEGL *window;
477
478   if ((gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_WAYLAND)
479       == 0)
480     /* we require a wayland display to create wayland surfaces */
481     return NULL;
482
483   GST_DEBUG ("creating Wayland EGL window");
484
485   window = g_object_new (GST_TYPE_GL_WINDOW_WAYLAND_EGL, NULL);
486   gst_object_ref_sink (window);
487
488   return window;
489 }
490
491 static void
492 gst_gl_window_wayland_egl_close (GstGLWindow * window)
493 {
494   GstGLWindowWaylandEGL *window_egl;
495
496   window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
497
498   destroy_surfaces (window_egl);
499
500   g_source_destroy (window_egl->wl_source);
501   g_source_unref (window_egl->wl_source);
502   window_egl->wl_source = NULL;
503
504   GST_GL_WINDOW_CLASS (parent_class)->close (window);
505 }
506
507 static gboolean
508 gst_gl_window_wayland_egl_open (GstGLWindow * window, GError ** error)
509 {
510   GstGLDisplayWayland *display;
511   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
512
513   if (!GST_IS_GL_DISPLAY_WAYLAND (window->display)) {
514     g_set_error (error, GST_GL_WINDOW_ERROR,
515         GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
516         "Failed to retrieve Wayland display (wrong type?)");
517     return FALSE;
518   }
519   display = GST_GL_DISPLAY_WAYLAND (window->display);
520
521   if (!display->display) {
522     g_set_error (error, GST_GL_WINDOW_ERROR,
523         GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
524         "Failed to retrieve Wayland display");
525     return FALSE;
526   }
527
528   window_egl->window.queue = wl_display_create_queue (display->display);
529
530   window_egl->wl_source = wayland_event_source_new (display->display,
531       window_egl->window.queue);
532
533   if (!GST_GL_WINDOW_CLASS (parent_class)->open (window, error))
534     return FALSE;
535
536   g_source_attach (window_egl->wl_source, window->main_context);
537
538   return TRUE;
539 }
540
541 void
542 gst_gl_window_wayland_egl_create_window (GstGLWindowWaylandEGL * window_egl)
543 {
544   create_surfaces (window_egl);
545 }
546
547 static guintptr
548 gst_gl_window_wayland_egl_get_window_handle (GstGLWindow * window)
549 {
550   return (guintptr) GST_GL_WINDOW_WAYLAND_EGL (window)->window.native;
551 }
552
553 static void
554 gst_gl_window_wayland_egl_set_window_handle (GstGLWindow * window,
555     guintptr handle)
556 {
557   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
558   struct wl_surface *surface = (struct wl_surface *) handle;
559
560   /* already set the NULL handle */
561   if (surface == NULL && window_egl->window.foreign_surface == NULL)
562     return;
563
564   /* unparent */
565   destroy_surfaces (window_egl);
566   window_egl->window.foreign_surface = surface;
567   create_surfaces (window_egl);
568 }
569
570 static void
571 _roundtrip_async (GstGLWindow * window)
572 {
573   GstGLDisplayWayland *display_wayland =
574       GST_GL_DISPLAY_WAYLAND (window->display);
575   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
576
577   create_surfaces (window_egl);
578
579   if (gst_gl_wl_display_roundtrip_queue (display_wayland->display,
580           window_egl->window.queue) < 0)
581     GST_WARNING_OBJECT (window, "failed a roundtrip");
582 }
583
584 static void
585 gst_gl_window_wayland_egl_show (GstGLWindow * window)
586 {
587   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
588
589   create_surfaces (window_egl);
590
591   gst_gl_window_send_message (window, (GstGLWindowCB) _roundtrip_async, window);
592 }
593
594 static void
595 window_resize (GstGLWindowWaylandEGL * window_egl, guint width, guint height)
596 {
597   GstGLWindow *window = GST_GL_WINDOW (window_egl);
598
599   GST_DEBUG ("resizing window from %ux%u to %ux%u",
600       window_egl->window.window_width, window_egl->window.window_height, width,
601       height);
602
603   if (window_egl->window.native) {
604     wl_egl_window_resize (window_egl->window.native, width, height, 0, 0);
605   }
606
607   gst_gl_window_resize (window, width, height);
608
609   window_egl->window.window_width = width;
610   window_egl->window.window_height = height;
611 }
612
613 static void
614 draw_cb (gpointer data)
615 {
616   GstGLWindowWaylandEGL *window_egl = data;
617   GstGLWindow *window = GST_GL_WINDOW (window_egl);
618   GstGLContext *context = gst_gl_window_get_context (window);
619
620   create_surfaces (window_egl);
621
622   if (window_egl->window.subsurface)
623     wl_subsurface_set_desync (window_egl->window.subsurface);
624
625   if (window->queue_resize) {
626     guint width, height;
627
628     gst_gl_window_get_surface_dimensions (window, &width, &height);
629     gst_gl_window_resize (window, width, height);
630   }
631
632   if (window->draw)
633     window->draw (window->draw_data);
634
635   gst_gl_context_swap_buffers (context);
636
637   if (window_egl->window.subsurface)
638     wl_subsurface_set_desync (window_egl->window.subsurface);
639
640   gst_object_unref (context);
641 }
642
643 static void
644 gst_gl_window_wayland_egl_draw (GstGLWindow * window)
645 {
646   gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, window);
647 }
648
649 struct SetRenderRectangle
650 {
651   GstGLWindowWaylandEGL *window_egl;
652   GstVideoRectangle rect;
653 };
654
655 static void
656 _free_set_render_rectangle (struct SetRenderRectangle *render)
657 {
658   if (render) {
659     if (render->window_egl)
660       gst_object_unref (render->window_egl);
661     g_free (render);
662   }
663 }
664
665 static void
666 _set_render_rectangle (gpointer data)
667 {
668   struct SetRenderRectangle *render = data;
669
670   GST_LOG_OBJECT (render->window_egl, "setting render rectangle %i,%i+%ix%i",
671       render->rect.x, render->rect.y, render->rect.w, render->rect.h);
672
673   if (render->window_egl->window.subsurface) {
674     wl_subsurface_set_sync (render->window_egl->window.subsurface);
675     wl_subsurface_set_position (render->window_egl->window.subsurface,
676         render->rect.x, render->rect.y);
677     render->window_egl->window.window_x = render->rect.x;
678     render->window_egl->window.window_y = render->rect.y;
679   }
680
681   window_resize (render->window_egl, render->rect.w, render->rect.h);
682
683   render->window_egl->window.render_rect = render->rect;
684 }
685
686 static gboolean
687 gst_gl_window_wayland_egl_set_render_rectangle (GstGLWindow * window,
688     gint x, gint y, gint width, gint height)
689 {
690   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
691   struct SetRenderRectangle *render;
692
693   render = g_new0 (struct SetRenderRectangle, 1);
694   render->window_egl = gst_object_ref (window_egl);
695   render->rect.x = x;
696   render->rect.y = y;
697   render->rect.w = width;
698   render->rect.h = height;
699
700   gst_gl_window_send_message_async (window,
701       (GstGLWindowCB) _set_render_rectangle, render,
702       (GDestroyNotify) _free_set_render_rectangle);
703
704   return TRUE;
705 }
706
707 static void
708 gst_gl_window_wayland_egl_set_preferred_size (GstGLWindow * window, gint width,
709     gint height)
710 {
711   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
712
713   window_egl->window.preferred_width = width;
714   window_egl->window.preferred_height = height;
715   if (window_egl->window.render_rect.w < 0
716       && window_egl->window.render_rect.h < 0) {
717     if (window_egl->window.window_height != height
718         || window_egl->window.window_width != width) {
719       window_resize (window_egl, width, height);
720     }
721   }
722 }
723
724 static guintptr
725 gst_gl_window_wayland_egl_get_display (GstGLWindow * window)
726 {
727   GstGLDisplayWayland *display = GST_GL_DISPLAY_WAYLAND (window->display);
728
729   return (guintptr) display->display;
730 }