• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
pointer_handle_enter(void * data,struct wl_pointer * pointer,uint32_t serial,struct wl_surface * surface,wl_fixed_t sx_w,wl_fixed_t sy_w)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
pointer_handle_leave(void * data,struct wl_pointer * pointer,uint32_t serial,struct wl_surface * surface)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
pointer_handle_motion(void * data,struct wl_pointer * pointer,uint32_t time,wl_fixed_t sx_w,wl_fixed_t sy_w)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
pointer_handle_button(void * data,struct wl_pointer * pointer,uint32_t serial,uint32_t time,uint32_t button,uint32_t state)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
pointer_handle_axis(void * data,struct wl_pointer * pointer,uint32_t time,uint32_t axis,wl_fixed_t value)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
seat_handle_capabilities(void * data,struct wl_seat * seat,enum wl_seat_capability caps)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
seat_name(void * data,struct wl_seat * seat,const char * name)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
handle_ping(void * data,struct wl_shell_surface * wl_shell_surface,uint32_t serial)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
handle_configure(void * data,struct wl_shell_surface * wl_shell_surface,uint32_t edges,int32_t width,int32_t height)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
handle_popup_done(void * data,struct wl_shell_surface * wl_shell_surface)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
handle_xdg_toplevel_close(void * data,struct xdg_toplevel * xdg_toplevel)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
handle_xdg_toplevel_configure(void * data,struct xdg_toplevel * xdg_toplevel,int32_t width,int32_t height,struct wl_array * states)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
handle_xdg_surface_configure(void * data,struct xdg_surface * xdg_surface,uint32_t serial)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
destroy_surfaces(GstGLWindowWaylandEGL * window_egl)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
create_xdg_surface_and_toplevel(GstGLWindowWaylandEGL * window_egl)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
create_wl_shell_surface(GstGLWindowWaylandEGL * window_egl)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
create_surfaces(GstGLWindowWaylandEGL * window_egl)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
gst_gl_window_wayland_egl_class_init(GstGLWindowWaylandEGLClass * klass)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
gst_gl_window_wayland_egl_init(GstGLWindowWaylandEGL * window)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 *
gst_gl_window_wayland_egl_new(GstGLDisplay * display)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
gst_gl_window_wayland_egl_close(GstGLWindow * gl_window)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   GST_GL_WINDOW_CLASS (parent_class)->close (gl_window);
482 }
483 
484 static void
handle_xdg_wm_base_ping(void * user_data,struct xdg_wm_base * xdg_wm_base,uint32_t serial)485 handle_xdg_wm_base_ping (void *user_data, struct xdg_wm_base *xdg_wm_base,
486     uint32_t serial)
487 {
488   xdg_wm_base_pong (xdg_wm_base, serial);
489 }
490 
491 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
492   handle_xdg_wm_base_ping
493 };
494 
495 static void
registry_handle_global(void * data,struct wl_registry * registry,uint32_t name,const char * interface,uint32_t version)496 registry_handle_global (void *data, struct wl_registry *registry,
497     uint32_t name, const char *interface, uint32_t version)
498 {
499   GstGLWindowWaylandEGL *window_wayland = data;
500 
501   GST_TRACE_OBJECT (window_wayland, "registry_handle_global with registry %p, "
502       "name %" G_GUINT32_FORMAT ", interface %s, version %u", registry, name,
503       interface, version);
504 
505   if (g_strcmp0 (interface, "wl_compositor") == 0) {
506     window_wayland->display.compositor =
507         wl_registry_bind (registry, name, &wl_compositor_interface, 1);
508   } else if (g_strcmp0 (interface, "wl_subcompositor") == 0) {
509     window_wayland->display.subcompositor =
510         wl_registry_bind (registry, name, &wl_subcompositor_interface, 1);
511   } else if (g_strcmp0 (interface, "xdg_wm_base") == 0) {
512     window_wayland->display.xdg_wm_base =
513         wl_registry_bind (registry, name, &xdg_wm_base_interface, 1);
514     xdg_wm_base_add_listener (window_wayland->display.xdg_wm_base,
515         &xdg_wm_base_listener, window_wayland);
516   } else if (g_strcmp0 (interface, "wl_shell") == 0) {
517     window_wayland->display.shell =
518         wl_registry_bind (registry, name, &wl_shell_interface, 1);
519   } else if (g_strcmp0 (interface, "wl_seat") == 0) {
520     window_wayland->display.seat =
521         wl_registry_bind (registry, name, &wl_seat_interface, 4);
522     wl_seat_add_listener (window_wayland->display.seat, &seat_listener,
523         window_wayland);
524   }
525 }
526 
527 static void
registry_handle_global_remove(void * data,struct wl_registry * registry,uint32_t name)528 registry_handle_global_remove (void *data, struct wl_registry *registry,
529     uint32_t name)
530 {
531   GstGLWindowWaylandEGL *window_wayland = data;
532 
533   /* TODO: deal with any registry objects that may be removed */
534   GST_TRACE_OBJECT (window_wayland, "wl_registry %p global_remove %"
535       G_GUINT32_FORMAT, registry, name);
536 }
537 
538 static const struct wl_registry_listener registry_listener = {
539   registry_handle_global,
540   registry_handle_global_remove,
541 };
542 
543 static gboolean
gst_gl_window_wayland_egl_open(GstGLWindow * window,GError ** error)544 gst_gl_window_wayland_egl_open (GstGLWindow * window, GError ** error)
545 {
546   GstGLDisplayWayland *display;
547   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
548 
549   if (!GST_IS_GL_DISPLAY_WAYLAND (window->display)) {
550     g_set_error (error, GST_GL_WINDOW_ERROR,
551         GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
552         "Failed to retrieve Wayland display (wrong type?)");
553     return FALSE;
554   }
555   display = GST_GL_DISPLAY_WAYLAND (window->display);
556 
557   if (!display->display) {
558     g_set_error (error, GST_GL_WINDOW_ERROR,
559         GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
560         "Failed to retrieve Wayland display");
561     return FALSE;
562   }
563 
564   /* we create a proxy wrapper for the display so that we can set the queue on
565    * it. This allows us to avoid sprinkling `wl_proxy_set_queue()` calls for
566    * each wayland resource we create as well as removing a race between
567    * creation and the `wl_proxy_set_queue()` call. */
568   window_egl->display.display = wl_proxy_create_wrapper (display->display);
569   window_egl->window.queue = wl_display_create_queue (display->display);
570 
571   wl_proxy_set_queue ((struct wl_proxy *) window_egl->display.display,
572       window_egl->window.queue);
573 
574   window_egl->display.registry =
575       wl_display_get_registry (window_egl->display.display);
576   wl_registry_add_listener (window_egl->display.registry, &registry_listener,
577       window_egl);
578 
579   if (wl_display_roundtrip_queue (display->display,
580           window_egl->window.queue) < 0) {
581     g_set_error (error, GST_GL_WINDOW_ERROR,
582         GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
583         "Failed to perform a wayland roundtrip");
584     return FALSE;
585   }
586 
587   window_egl->wl_source = wayland_event_source_new (display->display,
588       window_egl->window.queue);
589 
590   if (!GST_GL_WINDOW_CLASS (parent_class)->open (window, error))
591     return FALSE;
592 
593   g_source_attach (window_egl->wl_source, window->main_context);
594 
595   return TRUE;
596 }
597 
598 void
gst_gl_window_wayland_egl_create_window(GstGLWindowWaylandEGL * window_egl)599 gst_gl_window_wayland_egl_create_window (GstGLWindowWaylandEGL * window_egl)
600 {
601   create_surfaces (window_egl);
602 }
603 
604 static guintptr
gst_gl_window_wayland_egl_get_window_handle(GstGLWindow * window)605 gst_gl_window_wayland_egl_get_window_handle (GstGLWindow * window)
606 {
607   return (guintptr) GST_GL_WINDOW_WAYLAND_EGL (window)->window.native;
608 }
609 
610 static void
gst_gl_window_wayland_egl_set_window_handle(GstGLWindow * window,guintptr handle)611 gst_gl_window_wayland_egl_set_window_handle (GstGLWindow * window,
612     guintptr handle)
613 {
614   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
615   struct wl_surface *surface = (struct wl_surface *) handle;
616 
617   /* already set the NULL handle */
618   if (surface == NULL && window_egl->window.foreign_surface == NULL)
619     return;
620 
621   /* unparent */
622   destroy_surfaces (window_egl);
623   window_egl->window.foreign_surface = surface;
624   create_surfaces (window_egl);
625 }
626 
627 static void
_roundtrip_async(GstGLWindow * window)628 _roundtrip_async (GstGLWindow * window)
629 {
630   GstGLDisplayWayland *display = GST_GL_DISPLAY_WAYLAND (window->display);
631   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
632 
633   create_surfaces (window_egl);
634 
635   if (wl_display_roundtrip_queue (display->display,
636           window_egl->window.queue) < 0)
637     GST_WARNING_OBJECT (window, "failed a roundtrip");
638 }
639 
640 static void
gst_gl_window_wayland_egl_show(GstGLWindow * window)641 gst_gl_window_wayland_egl_show (GstGLWindow * window)
642 {
643   gst_gl_window_send_message (window, (GstGLWindowCB) _roundtrip_async, window);
644 }
645 
646 static void
window_resize(GstGLWindowWaylandEGL * window_egl,guint width,guint height)647 window_resize (GstGLWindowWaylandEGL * window_egl, guint width, guint height)
648 {
649   GstGLWindow *window = GST_GL_WINDOW (window_egl);
650 
651   GST_DEBUG ("resizing window from %ux%u to %ux%u",
652       window_egl->window.window_width, window_egl->window.window_height, width,
653       height);
654 
655   if (window_egl->window.native) {
656     wl_egl_window_resize (window_egl->window.native, width, height, 0, 0);
657   }
658 
659   gst_gl_window_resize (window, width, height);
660 
661   window_egl->window.window_width = width;
662   window_egl->window.window_height = height;
663 }
664 
665 static void
draw_cb(gpointer data)666 draw_cb (gpointer data)
667 {
668   GstGLWindowWaylandEGL *window_egl = data;
669   GstGLWindow *window = GST_GL_WINDOW (window_egl);
670   GstGLContext *context = gst_gl_window_get_context (window);
671 
672   create_surfaces (window_egl);
673 
674   if (window_egl->window.subsurface)
675     wl_subsurface_set_desync (window_egl->window.subsurface);
676 
677   if (window->queue_resize) {
678     guint width, height;
679 
680     gst_gl_window_get_surface_dimensions (window, &width, &height);
681     gst_gl_window_resize (window, width, height);
682   }
683 
684   if (window->draw)
685     window->draw (window->draw_data);
686 
687   gst_gl_context_swap_buffers (context);
688 
689   if (window_egl->window.subsurface)
690     wl_subsurface_set_desync (window_egl->window.subsurface);
691 
692   gst_object_unref (context);
693 }
694 
695 static void
gst_gl_window_wayland_egl_draw(GstGLWindow * window)696 gst_gl_window_wayland_egl_draw (GstGLWindow * window)
697 {
698   gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, window);
699 }
700 
701 struct SetRenderRectangle
702 {
703   GstGLWindowWaylandEGL *window_egl;
704   GstVideoRectangle rect;
705 };
706 
707 static void
_free_set_render_rectangle(struct SetRenderRectangle * render)708 _free_set_render_rectangle (struct SetRenderRectangle *render)
709 {
710   if (render) {
711     if (render->window_egl)
712       gst_object_unref (render->window_egl);
713     g_free (render);
714   }
715 }
716 
717 static void
_set_render_rectangle(gpointer data)718 _set_render_rectangle (gpointer data)
719 {
720   struct SetRenderRectangle *render = data;
721 
722   GST_LOG_OBJECT (render->window_egl, "setting render rectangle %i,%i+%ix%i",
723       render->rect.x, render->rect.y, render->rect.w, render->rect.h);
724 
725   if (render->window_egl->window.subsurface) {
726     wl_subsurface_set_sync (render->window_egl->window.subsurface);
727     wl_subsurface_set_position (render->window_egl->window.subsurface,
728         render->rect.x, render->rect.y);
729     render->window_egl->window.window_x = render->rect.x;
730     render->window_egl->window.window_y = render->rect.y;
731   }
732 
733   window_resize (render->window_egl, render->rect.w, render->rect.h);
734 
735   render->window_egl->window.render_rect = render->rect;
736 }
737 
738 static gboolean
gst_gl_window_wayland_egl_set_render_rectangle(GstGLWindow * window,gint x,gint y,gint width,gint height)739 gst_gl_window_wayland_egl_set_render_rectangle (GstGLWindow * window,
740     gint x, gint y, gint width, gint height)
741 {
742   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
743   struct SetRenderRectangle *render;
744 
745   render = g_new0 (struct SetRenderRectangle, 1);
746   render->window_egl = gst_object_ref (window_egl);
747   render->rect.x = x;
748   render->rect.y = y;
749   render->rect.w = width;
750   render->rect.h = height;
751 
752   gst_gl_window_send_message_async (window,
753       (GstGLWindowCB) _set_render_rectangle, render,
754       (GDestroyNotify) _free_set_render_rectangle);
755 
756   return TRUE;
757 }
758 
759 static void
gst_gl_window_wayland_egl_set_preferred_size(GstGLWindow * window,gint width,gint height)760 gst_gl_window_wayland_egl_set_preferred_size (GstGLWindow * window, gint width,
761     gint height)
762 {
763   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
764 
765   window_egl->window.preferred_width = width;
766   window_egl->window.preferred_height = height;
767   if (window_egl->window.render_rect.w < 0
768       && window_egl->window.render_rect.h < 0) {
769     if (window_egl->window.window_height != height
770         || window_egl->window.window_width != width) {
771       window_resize (window_egl, width, height);
772     }
773   }
774 }
775 
776 static guintptr
gst_gl_window_wayland_egl_get_display(GstGLWindow * window)777 gst_gl_window_wayland_egl_get_display (GstGLWindow * window)
778 {
779   GstGLDisplayWayland *display = GST_GL_DISPLAY_WAYLAND (window->display);
780 
781   return (guintptr) display->display;
782 }
783