3 * Copyright (C) 2008 Julien Isorce <julien.isorce@gmail.com>
4 * Copyright (C) 2012 Matthew Waters <ystreet00@gmail.com>
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.
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.
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.
22 #define GLIB_DISABLE_DEPRECATION_WARNINGS
28 #include <linux/input.h>
30 #include "wayland_event_source.h"
32 #include "../gstgl_fwd.h"
33 #include <gst/gl/gstglcontext.h>
35 #include "gstgldisplay_wayland.h"
36 #include "gstgldisplay_wayland_private.h"
37 #include "gstglwindow_wayland_egl.h"
39 #include "../gstglwindow_private.h"
41 const gchar *WlEGLErrorString ();
43 #define GST_CAT_DEFAULT gst_gl_window_debug
45 #define gst_gl_window_wayland_egl_parent_class parent_class
46 G_DEFINE_TYPE (GstGLWindowWaylandEGL, gst_gl_window_wayland_egl,
49 static guintptr gst_gl_window_wayland_egl_get_window_handle (GstGLWindow *
51 static void gst_gl_window_wayland_egl_set_window_handle (GstGLWindow * window,
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,
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);
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)
69 GstGLWindowWaylandEGL *window_egl = data;
70 struct wl_buffer *buffer;
71 struct wl_cursor_image *image = NULL;
73 window_egl->display.serial = serial;
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);
88 pointer_handle_leave (void *data, struct wl_pointer *pointer, uint32_t serial,
89 struct wl_surface *surface)
91 GstGLWindowWaylandEGL *window_egl = data;
92 window_egl->display.serial = serial;
96 pointer_handle_motion (void *data, struct wl_pointer *pointer, uint32_t time,
97 wl_fixed_t sx_w, wl_fixed_t sy_w)
99 GstGLWindowWaylandEGL *window_egl = data;
101 window_egl->display.pointer_x = wl_fixed_to_double (sx_w);
102 window_egl->display.pointer_y = wl_fixed_to_double (sy_w);
115 _get_closest_pointer_corner (GstGLWindowWaylandEGL * window_egl)
118 gdouble win_width, win_height;
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;
126 if (win_width == 0.0 || win_height == 0.0)
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;
136 pointer_handle_button (void *data, struct wl_pointer *pointer, uint32_t serial,
137 uint32_t time, uint32_t button, uint32_t state_w)
139 GstGLWindowWaylandEGL *window_egl = data;
140 guint edges = _get_closest_pointer_corner (window_egl);
141 window_egl->display.serial = serial;
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);
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);
153 pointer_handle_axis (void *data, struct wl_pointer *pointer, uint32_t time,
154 uint32_t axis, wl_fixed_t value)
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,
167 seat_handle_capabilities (void *data, struct wl_seat *seat,
168 enum wl_seat_capability caps)
170 GstGLWindowWaylandEGL *window_egl = data;
171 struct display *display = &window_egl->display;
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;
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;
193 static const struct wl_seat_listener seat_listener = {
194 seat_handle_capabilities,
198 handle_ping (void *data, struct wl_shell_surface *wl_shell_surface,
201 GstGLWindowWaylandEGL *window_egl = data;
203 GST_TRACE_OBJECT (window_egl, "ping received serial %u", serial);
205 wl_shell_surface_pong (wl_shell_surface, serial);
208 static void window_resize (GstGLWindowWaylandEGL * window_egl, guint width,
212 handle_configure (void *data, struct wl_shell_surface *wl_shell_surface,
213 uint32_t edges, int32_t width, int32_t height)
215 GstGLWindowWaylandEGL *window_egl = data;
217 GST_DEBUG ("configure event on surface %p, %ix%i", wl_shell_surface, width,
220 window_resize (window_egl, width, height);
224 handle_popup_done (void *data, struct wl_shell_surface *wl_shell_surface)
228 static const struct wl_shell_surface_listener wl_shell_surface_listener = {
235 handle_xdg_toplevel_close (void *data, struct xdg_toplevel *xdg_toplevel)
237 GstGLWindowWaylandEGL *window_egl = data;
239 GST_DEBUG ("XDG toplevel got a \"close\" event.");
241 gst_gl_window_wayland_egl_close (GST_GL_WINDOW (window_egl));
245 handle_xdg_toplevel_configure (void *data, struct xdg_toplevel *xdg_toplevel,
246 int32_t width, int32_t height, struct wl_array *states)
248 GstGLWindowWaylandEGL *window_egl = data;
249 const uint32_t *state;
251 GST_DEBUG ("configure event on XDG toplevel %p, %ix%i", xdg_toplevel,
254 wl_array_for_each (state, states) {
256 case XDG_TOPLEVEL_STATE_FULLSCREEN:
257 window_egl->window.fullscreen = TRUE;
259 case XDG_TOPLEVEL_STATE_MAXIMIZED:
260 case XDG_TOPLEVEL_STATE_RESIZING:
261 case XDG_TOPLEVEL_STATE_ACTIVATED:
266 if (width > 0 && height > 0)
267 window_resize (window_egl, width, height);
270 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
271 handle_xdg_toplevel_configure,
272 handle_xdg_toplevel_close,
276 handle_xdg_surface_configure (void *data, struct xdg_surface *xdg_surface,
279 xdg_surface_ack_configure (xdg_surface, serial);
282 static const struct xdg_surface_listener xdg_surface_listener = {
283 handle_xdg_surface_configure,
287 destroy_surfaces (GstGLWindowWaylandEGL * window_egl)
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);
299 create_xdg_surface_and_toplevel (GstGLWindowWaylandEGL * window_egl)
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;
307 GST_DEBUG ("Creating surfaces XDG-shell");
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);
317 xdg_surface_add_listener (xdg_surface, &xdg_surface_listener, window_egl);
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);
326 xdg_toplevel_add_listener (xdg_toplevel, &xdg_toplevel_listener, window_egl);
328 /* Commit the xdg_surface state */
329 wl_surface_commit (window_egl->window.surface);
331 /* And save them into the fields */
332 window_egl->window.xdg_surface = xdg_surface;
333 window_egl->window.xdg_toplevel = xdg_toplevel;
337 create_wl_shell_surface (GstGLWindowWaylandEGL * window_egl)
339 GstGLDisplayWayland *display =
340 GST_GL_DISPLAY_WAYLAND (GST_GL_WINDOW (window_egl)->display);
341 struct wl_shell_surface *wl_shell_surface;
343 GST_DEBUG ("Creating surfaces for wl-shell");
345 wl_shell_surface = wl_shell_get_shell_surface (display->wl_shell,
346 window_egl->window.surface);
348 if (window_egl->window.queue) {
349 wl_proxy_set_queue ((struct wl_proxy *) wl_shell_surface,
350 window_egl->window.queue);
353 wl_shell_surface_add_listener (wl_shell_surface, &wl_shell_surface_listener,
355 wl_shell_surface_set_title (wl_shell_surface, "OpenGL Renderer");
356 wl_shell_surface_set_toplevel (wl_shell_surface);
358 window_egl->window.wl_shell_surface = wl_shell_surface;
362 create_surfaces (GstGLWindowWaylandEGL * window_egl)
364 GstGLDisplayWayland *display =
365 GST_GL_DISPLAY_WAYLAND (GST_GL_WINDOW (window_egl)->display);
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);
376 if (window_egl->window.foreign_surface) {
378 if (!display->subcompositor) {
379 GST_ERROR_OBJECT (window_egl,
380 "Wayland server does not support subsurfaces");
381 window_egl->window.foreign_surface = NULL;
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);
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);
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);
408 * render_rect is the application requested size so choose that first if
410 * Else choose the already chosen size if set
411 * Else choose the preferred size if set
412 * Else choose a default value
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;
422 window_egl->window.window_width = width;
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;
432 window_egl->window.window_height = height;
434 if (!window_egl->window.native) {
435 gst_gl_window_resize (GST_GL_WINDOW (window_egl), width, height);
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);
446 gst_gl_window_wayland_egl_class_init (GstGLWindowWaylandEGLClass * klass)
448 GstGLWindowClass *window_class = (GstGLWindowClass *) klass;
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);
467 gst_gl_window_wayland_egl_init (GstGLWindowWaylandEGL * window)
469 window->window.render_rect.w = window->window.render_rect.h = -1;
472 /* Must be called in the gl thread */
473 GstGLWindowWaylandEGL *
474 gst_gl_window_wayland_egl_new (GstGLDisplay * display)
476 GstGLWindowWaylandEGL *window;
478 if ((gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_WAYLAND)
480 /* we require a wayland display to create wayland surfaces */
483 GST_DEBUG ("creating Wayland EGL window");
485 window = g_object_new (GST_TYPE_GL_WINDOW_WAYLAND_EGL, NULL);
486 gst_object_ref_sink (window);
492 gst_gl_window_wayland_egl_close (GstGLWindow * window)
494 GstGLWindowWaylandEGL *window_egl;
496 window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
498 destroy_surfaces (window_egl);
500 g_source_destroy (window_egl->wl_source);
501 g_source_unref (window_egl->wl_source);
502 window_egl->wl_source = NULL;
504 GST_GL_WINDOW_CLASS (parent_class)->close (window);
508 gst_gl_window_wayland_egl_open (GstGLWindow * window, GError ** error)
510 GstGLDisplayWayland *display;
511 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
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?)");
519 display = GST_GL_DISPLAY_WAYLAND (window->display);
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");
528 window_egl->window.queue = wl_display_create_queue (display->display);
530 window_egl->wl_source = wayland_event_source_new (display->display,
531 window_egl->window.queue);
533 if (!GST_GL_WINDOW_CLASS (parent_class)->open (window, error))
536 g_source_attach (window_egl->wl_source, window->main_context);
542 gst_gl_window_wayland_egl_create_window (GstGLWindowWaylandEGL * window_egl)
544 create_surfaces (window_egl);
548 gst_gl_window_wayland_egl_get_window_handle (GstGLWindow * window)
550 return (guintptr) GST_GL_WINDOW_WAYLAND_EGL (window)->window.native;
554 gst_gl_window_wayland_egl_set_window_handle (GstGLWindow * window,
557 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
558 struct wl_surface *surface = (struct wl_surface *) handle;
560 /* already set the NULL handle */
561 if (surface == NULL && window_egl->window.foreign_surface == NULL)
565 destroy_surfaces (window_egl);
566 window_egl->window.foreign_surface = surface;
567 create_surfaces (window_egl);
571 _roundtrip_async (GstGLWindow * window)
573 GstGLDisplayWayland *display_wayland =
574 GST_GL_DISPLAY_WAYLAND (window->display);
575 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
577 create_surfaces (window_egl);
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");
585 gst_gl_window_wayland_egl_show (GstGLWindow * window)
587 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
589 create_surfaces (window_egl);
591 gst_gl_window_send_message (window, (GstGLWindowCB) _roundtrip_async, window);
595 window_resize (GstGLWindowWaylandEGL * window_egl, guint width, guint height)
597 GstGLWindow *window = GST_GL_WINDOW (window_egl);
599 GST_DEBUG ("resizing window from %ux%u to %ux%u",
600 window_egl->window.window_width, window_egl->window.window_height, width,
603 if (window_egl->window.native) {
604 wl_egl_window_resize (window_egl->window.native, width, height, 0, 0);
607 gst_gl_window_resize (window, width, height);
609 window_egl->window.window_width = width;
610 window_egl->window.window_height = height;
614 draw_cb (gpointer data)
616 GstGLWindowWaylandEGL *window_egl = data;
617 GstGLWindow *window = GST_GL_WINDOW (window_egl);
618 GstGLContext *context = gst_gl_window_get_context (window);
620 create_surfaces (window_egl);
622 if (window_egl->window.subsurface)
623 wl_subsurface_set_desync (window_egl->window.subsurface);
625 if (window->queue_resize) {
628 gst_gl_window_get_surface_dimensions (window, &width, &height);
629 gst_gl_window_resize (window, width, height);
633 window->draw (window->draw_data);
635 gst_gl_context_swap_buffers (context);
637 if (window_egl->window.subsurface)
638 wl_subsurface_set_desync (window_egl->window.subsurface);
640 gst_object_unref (context);
644 gst_gl_window_wayland_egl_draw (GstGLWindow * window)
646 gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, window);
649 struct SetRenderRectangle
651 GstGLWindowWaylandEGL *window_egl;
652 GstVideoRectangle rect;
656 _free_set_render_rectangle (struct SetRenderRectangle *render)
659 if (render->window_egl)
660 gst_object_unref (render->window_egl);
666 _set_render_rectangle (gpointer data)
668 struct SetRenderRectangle *render = data;
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);
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;
681 window_resize (render->window_egl, render->rect.w, render->rect.h);
683 render->window_egl->window.render_rect = render->rect;
687 gst_gl_window_wayland_egl_set_render_rectangle (GstGLWindow * window,
688 gint x, gint y, gint width, gint height)
690 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
691 struct SetRenderRectangle *render;
693 render = g_new0 (struct SetRenderRectangle, 1);
694 render->window_egl = gst_object_ref (window_egl);
697 render->rect.w = width;
698 render->rect.h = height;
700 gst_gl_window_send_message_async (window,
701 (GstGLWindowCB) _set_render_rectangle, render,
702 (GDestroyNotify) _free_set_render_rectangle);
708 gst_gl_window_wayland_egl_set_preferred_size (GstGLWindow * window, gint width,
711 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
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);
725 gst_gl_window_wayland_egl_get_display (GstGLWindow * window)
727 GstGLDisplayWayland *display = GST_GL_DISPLAY_WAYLAND (window->display);
729 return (guintptr) display->display;