• 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 <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
handle_ping(void * data,struct wl_shell_surface * wl_shell_surface,uint32_t serial)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
handle_configure(void * data,struct wl_shell_surface * wl_shell_surface,uint32_t edges,int32_t width,int32_t height)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
handle_popup_done(void * data,struct wl_shell_surface * wl_shell_surface)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
handle_xdg_toplevel_close(void * data,struct xdg_toplevel * xdg_toplevel)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
handle_xdg_toplevel_configure(void * data,struct xdg_toplevel * xdg_toplevel,int32_t width,int32_t height,struct wl_array * states)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
handle_xdg_surface_configure(void * data,struct xdg_surface * xdg_surface,uint32_t serial)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
destroy_surfaces(GstGLWindowWaylandEGL * window_egl)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
create_xdg_surface_and_toplevel(GstGLWindowWaylandEGL * window_egl)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   if (window_egl->window.queue) {
312     wl_proxy_set_queue ((struct wl_proxy *) xdg_wm_base,
313         window_egl->window.queue);
314   }
315   xdg_surface = xdg_wm_base_get_xdg_surface (xdg_wm_base,
316       window_egl->window.surface);
317   if (window_egl->window.queue) {
318     wl_proxy_set_queue ((struct wl_proxy *) xdg_surface,
319         window_egl->window.queue);
320   }
321   xdg_surface_add_listener (xdg_surface, &xdg_surface_listener, window_egl);
322 
323   /* Then the XDG top-level */
324   xdg_toplevel = xdg_surface_get_toplevel (xdg_surface);
325   xdg_toplevel_set_title (xdg_toplevel, "OpenGL Renderer");
326   if (window_egl->window.queue) {
327     wl_proxy_set_queue ((struct wl_proxy *) xdg_toplevel,
328         window_egl->window.queue);
329   }
330   xdg_toplevel_add_listener (xdg_toplevel, &xdg_toplevel_listener, window_egl);
331 
332   /* Commit the xdg_surface state */
333   wl_surface_commit (window_egl->window.surface);
334 
335   /* And save them into the fields */
336   window_egl->window.xdg_surface = xdg_surface;
337   window_egl->window.xdg_toplevel = xdg_toplevel;
338 }
339 
340 static void
create_wl_shell_surface(GstGLWindowWaylandEGL * window_egl)341 create_wl_shell_surface (GstGLWindowWaylandEGL * window_egl)
342 {
343   GstGLDisplayWayland *display =
344       GST_GL_DISPLAY_WAYLAND (GST_GL_WINDOW (window_egl)->display);
345   struct wl_shell_surface *wl_shell_surface;
346 
347   GST_DEBUG ("Creating surfaces for wl-shell");
348 
349   wl_shell_surface = wl_shell_get_shell_surface (display->shell,
350       window_egl->window.surface);
351 
352   if (window_egl->window.queue) {
353     wl_proxy_set_queue ((struct wl_proxy *) wl_shell_surface,
354         window_egl->window.queue);
355   }
356 
357   wl_shell_surface_add_listener (wl_shell_surface, &wl_shell_surface_listener,
358       window_egl);
359   wl_shell_surface_set_title (wl_shell_surface, "OpenGL Renderer");
360   wl_shell_surface_set_toplevel (wl_shell_surface);
361 
362   window_egl->window.wl_shell_surface = wl_shell_surface;
363 }
364 
365 static void
create_surfaces(GstGLWindowWaylandEGL * window_egl)366 create_surfaces (GstGLWindowWaylandEGL * window_egl)
367 {
368   GstGLDisplayWayland *display =
369       GST_GL_DISPLAY_WAYLAND (GST_GL_WINDOW (window_egl)->display);
370   gint width, height;
371 
372   if (!window_egl->window.surface) {
373     window_egl->window.surface =
374         wl_compositor_create_surface (display->compositor);
375     if (window_egl->window.queue)
376       wl_proxy_set_queue ((struct wl_proxy *) window_egl->window.surface,
377           window_egl->window.queue);
378   }
379 
380   if (window_egl->window.foreign_surface) {
381     /* (re)parent */
382     if (!display->subcompositor) {
383       GST_ERROR_OBJECT (window_egl,
384           "Wayland server does not support subsurfaces");
385       window_egl->window.foreign_surface = NULL;
386       goto shell_window;
387     }
388 
389     if (!window_egl->window.subsurface) {
390       window_egl->window.subsurface =
391           wl_subcompositor_get_subsurface (display->subcompositor,
392           window_egl->window.surface, window_egl->window.foreign_surface);
393       if (window_egl->window.queue)
394         wl_proxy_set_queue ((struct wl_proxy *) window_egl->window.subsurface,
395             window_egl->window.queue);
396 
397       wl_subsurface_set_position (window_egl->window.subsurface,
398           window_egl->window.window_x, window_egl->window.window_y);
399       wl_subsurface_set_desync (window_egl->window.subsurface);
400     }
401   } else {
402   shell_window:
403     if (gst_gl_display_wayland_get_xdg_wm_base (display)) {
404       if (!window_egl->window.xdg_surface)
405         create_xdg_surface_and_toplevel (window_egl);
406     } else if (!window_egl->window.wl_shell_surface) {
407       create_wl_shell_surface (window_egl);
408     }
409   }
410 
411   /*
412    * render_rect is the application requested size so choose that first if
413    * available.
414    * Else choose the already chosen size if set
415    * Else choose the preferred size if set
416    * Else choose a default value
417    */
418   if (window_egl->window.render_rect.w > 0)
419     width = window_egl->window.render_rect.w;
420   else if (window_egl->window.window_width > 0)
421     width = window_egl->window.window_width;
422   else if (window_egl->window.preferred_width > 0)
423     width = window_egl->window.preferred_width;
424   else
425     width = 320;
426   window_egl->window.window_width = width;
427 
428   if (window_egl->window.render_rect.h > 0)
429     height = window_egl->window.render_rect.h;
430   else if (window_egl->window.window_height > 0)
431     height = window_egl->window.window_height;
432   else if (window_egl->window.preferred_height > 0)
433     height = window_egl->window.preferred_height;
434   else
435     height = 240;
436   window_egl->window.window_height = height;
437 
438   if (!window_egl->window.native) {
439     gst_gl_window_resize (GST_GL_WINDOW (window_egl), width, height);
440 
441     window_egl->window.native =
442         wl_egl_window_create (window_egl->window.surface, width, height);
443     if (window_egl->window.queue)
444       wl_proxy_set_queue ((struct wl_proxy *) window_egl->window.native,
445           window_egl->window.queue);
446   }
447 }
448 
449 static void
gst_gl_window_wayland_egl_class_init(GstGLWindowWaylandEGLClass * klass)450 gst_gl_window_wayland_egl_class_init (GstGLWindowWaylandEGLClass * klass)
451 {
452   GstGLWindowClass *window_class = (GstGLWindowClass *) klass;
453 
454   window_class->get_window_handle =
455       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_get_window_handle);
456   window_class->set_window_handle =
457       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_window_handle);
458   window_class->show = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_show);
459   window_class->draw = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_draw);
460   window_class->close = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_close);
461   window_class->open = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_open);
462   window_class->get_display =
463       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_get_display);
464   window_class->set_render_rectangle =
465       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_render_rectangle);
466   window_class->set_preferred_size =
467       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_preferred_size);
468 }
469 
470 static void
gst_gl_window_wayland_egl_init(GstGLWindowWaylandEGL * window)471 gst_gl_window_wayland_egl_init (GstGLWindowWaylandEGL * window)
472 {
473   window->window.render_rect.w = window->window.render_rect.h = -1;
474 }
475 
476 /* Must be called in the gl thread */
477 GstGLWindowWaylandEGL *
gst_gl_window_wayland_egl_new(GstGLDisplay * display)478 gst_gl_window_wayland_egl_new (GstGLDisplay * display)
479 {
480   GstGLWindowWaylandEGL *window;
481 
482   if ((gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_WAYLAND)
483       == 0)
484     /* we require a wayland display to create wayland surfaces */
485     return NULL;
486 
487   GST_DEBUG ("creating Wayland EGL window");
488 
489   window = g_object_new (GST_TYPE_GL_WINDOW_WAYLAND_EGL, NULL);
490   gst_object_ref_sink (window);
491 
492   return window;
493 }
494 
495 static void
gst_gl_window_wayland_egl_close(GstGLWindow * window)496 gst_gl_window_wayland_egl_close (GstGLWindow * window)
497 {
498   GstGLWindowWaylandEGL *window_egl;
499 
500   window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
501 
502   destroy_surfaces (window_egl);
503 
504   g_source_destroy (window_egl->wl_source);
505   g_source_unref (window_egl->wl_source);
506   window_egl->wl_source = NULL;
507 
508   GST_GL_WINDOW_CLASS (parent_class)->close (window);
509 }
510 
511 static gboolean
gst_gl_window_wayland_egl_open(GstGLWindow * window,GError ** error)512 gst_gl_window_wayland_egl_open (GstGLWindow * window, GError ** error)
513 {
514   GstGLDisplayWayland *display;
515   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
516 
517   if (!GST_IS_GL_DISPLAY_WAYLAND (window->display)) {
518     g_set_error (error, GST_GL_WINDOW_ERROR,
519         GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
520         "Failed to retrieve Wayland display (wrong type?)");
521     return FALSE;
522   }
523   display = GST_GL_DISPLAY_WAYLAND (window->display);
524 
525   if (!display->display) {
526     g_set_error (error, GST_GL_WINDOW_ERROR,
527         GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
528         "Failed to retrieve Wayland display");
529     return FALSE;
530   }
531 
532   window_egl->window.queue = wl_display_create_queue (display->display);
533 
534   window_egl->wl_source = wayland_event_source_new (display->display,
535       window_egl->window.queue);
536 
537   if (!GST_GL_WINDOW_CLASS (parent_class)->open (window, error))
538     return FALSE;
539 
540   g_source_attach (window_egl->wl_source, window->main_context);
541 
542   return TRUE;
543 }
544 
545 void
gst_gl_window_wayland_egl_create_window(GstGLWindowWaylandEGL * window_egl)546 gst_gl_window_wayland_egl_create_window (GstGLWindowWaylandEGL * window_egl)
547 {
548   create_surfaces (window_egl);
549 }
550 
551 static guintptr
gst_gl_window_wayland_egl_get_window_handle(GstGLWindow * window)552 gst_gl_window_wayland_egl_get_window_handle (GstGLWindow * window)
553 {
554   return (guintptr) GST_GL_WINDOW_WAYLAND_EGL (window)->window.native;
555 }
556 
557 static void
gst_gl_window_wayland_egl_set_window_handle(GstGLWindow * window,guintptr handle)558 gst_gl_window_wayland_egl_set_window_handle (GstGLWindow * window,
559     guintptr handle)
560 {
561   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
562   struct wl_surface *surface = (struct wl_surface *) handle;
563 
564   /* already set the NULL handle */
565   if (surface == NULL && window_egl->window.foreign_surface == NULL)
566     return;
567 
568   /* unparent */
569   destroy_surfaces (window_egl);
570   window_egl->window.foreign_surface = surface;
571   create_surfaces (window_egl);
572 }
573 
574 static void
_roundtrip_async(GstGLWindow * window)575 _roundtrip_async (GstGLWindow * window)
576 {
577   GstGLDisplayWayland *display_wayland =
578       GST_GL_DISPLAY_WAYLAND (window->display);
579   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
580 
581   create_surfaces (window_egl);
582 
583   if (gst_gl_wl_display_roundtrip_queue (display_wayland->display,
584           window_egl->window.queue) < 0)
585     GST_WARNING_OBJECT (window, "failed a roundtrip");
586 }
587 
588 static void
gst_gl_window_wayland_egl_show(GstGLWindow * window)589 gst_gl_window_wayland_egl_show (GstGLWindow * window)
590 {
591   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
592 
593   create_surfaces (window_egl);
594 
595   gst_gl_window_send_message (window, (GstGLWindowCB) _roundtrip_async, window);
596 }
597 
598 static void
window_resize(GstGLWindowWaylandEGL * window_egl,guint width,guint height)599 window_resize (GstGLWindowWaylandEGL * window_egl, guint width, guint height)
600 {
601   GstGLWindow *window = GST_GL_WINDOW (window_egl);
602 
603   GST_DEBUG ("resizing window from %ux%u to %ux%u",
604       window_egl->window.window_width, window_egl->window.window_height, width,
605       height);
606 
607   if (window_egl->window.native) {
608     wl_egl_window_resize (window_egl->window.native, width, height, 0, 0);
609   }
610 
611   gst_gl_window_resize (window, width, height);
612 
613   window_egl->window.window_width = width;
614   window_egl->window.window_height = height;
615 }
616 
617 static void
draw_cb(gpointer data)618 draw_cb (gpointer data)
619 {
620   GstGLWindowWaylandEGL *window_egl = data;
621   GstGLWindow *window = GST_GL_WINDOW (window_egl);
622   GstGLContext *context = gst_gl_window_get_context (window);
623 
624   create_surfaces (window_egl);
625 
626   if (window_egl->window.subsurface)
627     wl_subsurface_set_desync (window_egl->window.subsurface);
628 
629   if (window->queue_resize) {
630     guint width, height;
631 
632     gst_gl_window_get_surface_dimensions (window, &width, &height);
633     gst_gl_window_resize (window, width, height);
634   }
635 
636   if (window->draw)
637     window->draw (window->draw_data);
638 
639   gst_gl_context_swap_buffers (context);
640 
641   if (window_egl->window.subsurface)
642     wl_subsurface_set_desync (window_egl->window.subsurface);
643 
644   gst_object_unref (context);
645 }
646 
647 static void
gst_gl_window_wayland_egl_draw(GstGLWindow * window)648 gst_gl_window_wayland_egl_draw (GstGLWindow * window)
649 {
650   gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, window);
651 }
652 
653 struct SetRenderRectangle
654 {
655   GstGLWindowWaylandEGL *window_egl;
656   GstVideoRectangle rect;
657 };
658 
659 static void
_free_set_render_rectangle(struct SetRenderRectangle * render)660 _free_set_render_rectangle (struct SetRenderRectangle *render)
661 {
662   if (render) {
663     if (render->window_egl)
664       gst_object_unref (render->window_egl);
665     g_free (render);
666   }
667 }
668 
669 static void
_set_render_rectangle(gpointer data)670 _set_render_rectangle (gpointer data)
671 {
672   struct SetRenderRectangle *render = data;
673 
674   GST_LOG_OBJECT (render->window_egl, "setting render rectangle %i,%i+%ix%i",
675       render->rect.x, render->rect.y, render->rect.w, render->rect.h);
676 
677   if (render->window_egl->window.subsurface) {
678     wl_subsurface_set_sync (render->window_egl->window.subsurface);
679     wl_subsurface_set_position (render->window_egl->window.subsurface,
680         render->rect.x, render->rect.y);
681     render->window_egl->window.window_x = render->rect.x;
682     render->window_egl->window.window_y = render->rect.y;
683   }
684 
685   window_resize (render->window_egl, render->rect.w, render->rect.h);
686 
687   render->window_egl->window.render_rect = render->rect;
688 }
689 
690 static gboolean
gst_gl_window_wayland_egl_set_render_rectangle(GstGLWindow * window,gint x,gint y,gint width,gint height)691 gst_gl_window_wayland_egl_set_render_rectangle (GstGLWindow * window,
692     gint x, gint y, gint width, gint height)
693 {
694   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
695   struct SetRenderRectangle *render;
696 
697   render = g_new0 (struct SetRenderRectangle, 1);
698   render->window_egl = gst_object_ref (window_egl);
699   render->rect.x = x;
700   render->rect.y = y;
701   render->rect.w = width;
702   render->rect.h = height;
703 
704   gst_gl_window_send_message_async (window,
705       (GstGLWindowCB) _set_render_rectangle, render,
706       (GDestroyNotify) _free_set_render_rectangle);
707 
708   return TRUE;
709 }
710 
711 static void
gst_gl_window_wayland_egl_set_preferred_size(GstGLWindow * window,gint width,gint height)712 gst_gl_window_wayland_egl_set_preferred_size (GstGLWindow * window, gint width,
713     gint height)
714 {
715   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
716 
717   window_egl->window.preferred_width = width;
718   window_egl->window.preferred_height = height;
719   if (window_egl->window.render_rect.w < 0
720       && window_egl->window.render_rect.h < 0) {
721     if (window_egl->window.window_height != height
722         || window_egl->window.window_width != width) {
723       window_resize (window_egl, width, height);
724     }
725   }
726 }
727 
728 static guintptr
gst_gl_window_wayland_egl_get_display(GstGLWindow * window)729 gst_gl_window_wayland_egl_get_display (GstGLWindow * window)
730 {
731   GstGLDisplayWayland *display = GST_GL_DISPLAY_WAYLAND (window->display);
732 
733   return (guintptr) display->display;
734 }
735