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 "gstglwindow_wayland_egl.h"
38 #include "../gstglwindow_private.h"
40 const gchar *WlEGLErrorString ();
42 #define GST_CAT_DEFAULT gst_gl_window_debug
44 #define gst_gl_window_wayland_egl_parent_class parent_class
45 G_DEFINE_TYPE (GstGLWindowWaylandEGL, gst_gl_window_wayland_egl,
48 static guintptr gst_gl_window_wayland_egl_get_window_handle (GstGLWindow *
50 static void gst_gl_window_wayland_egl_set_window_handle (GstGLWindow * window,
52 static void gst_gl_window_wayland_egl_show (GstGLWindow * window);
53 static void gst_gl_window_wayland_egl_draw (GstGLWindow * window);
54 static void gst_gl_window_wayland_egl_close (GstGLWindow * window);
55 static gboolean gst_gl_window_wayland_egl_open (GstGLWindow * window,
57 static guintptr gst_gl_window_wayland_egl_get_display (GstGLWindow * window);
58 static gboolean gst_gl_window_wayland_egl_set_render_rectangle (GstGLWindow *
59 window, gint x, gint y, gint width, gint height);
60 static void gst_gl_window_wayland_egl_set_preferred_size (GstGLWindow * window,
61 gint width, gint height);
65 pointer_handle_enter (void *data, struct wl_pointer *pointer, uint32_t serial,
66 struct wl_surface *surface, wl_fixed_t sx_w, wl_fixed_t sy_w)
68 GstGLWindowWaylandEGL *window_egl = data;
69 struct wl_buffer *buffer;
70 struct wl_cursor_image *image = NULL;
72 window_egl->display.serial = serial;
74 if (window_egl->display.default_cursor) {
75 image = window_egl->display.default_cursor->images[0];
76 buffer = wl_cursor_image_get_buffer (image);
77 wl_pointer_set_cursor (pointer, serial,
78 window_egl->display.cursor_surface, image->hotspot_x, image->hotspot_y);
79 wl_surface_attach (window_egl->display.cursor_surface, buffer, 0, 0);
80 wl_surface_damage (window_egl->display.cursor_surface, 0, 0,
81 image->width, image->height);
82 wl_surface_commit (window_egl->display.cursor_surface);
87 pointer_handle_leave (void *data, struct wl_pointer *pointer, uint32_t serial,
88 struct wl_surface *surface)
90 GstGLWindowWaylandEGL *window_egl = data;
91 window_egl->display.serial = serial;
95 pointer_handle_motion (void *data, struct wl_pointer *pointer, uint32_t time,
96 wl_fixed_t sx_w, wl_fixed_t sy_w)
98 GstGLWindowWaylandEGL *window_egl = data;
100 window_egl->display.pointer_x = wl_fixed_to_double (sx_w);
101 window_egl->display.pointer_y = wl_fixed_to_double (sy_w);
114 _get_closest_pointer_corner (GstGLWindowWaylandEGL * window_egl)
117 gdouble win_width, win_height;
120 win_width = (gdouble) window_egl->window.window_width;
121 win_height = (gdouble) window_egl->window.window_height;
122 p_x = window_egl->display.pointer_x;
123 p_y = window_egl->display.pointer_y;
125 if (win_width == 0.0 || win_height == 0.0)
128 edges |= win_width / 2.0 - p_x < 0.0 ? WL_EDGE_RIGHT : WL_EDGE_LEFT;
129 edges |= win_height / 2.0 - p_y < 0.0 ? WL_EDGE_BOTTOM : WL_EDGE_TOP;
135 pointer_handle_button (void *data, struct wl_pointer *pointer, uint32_t serial,
136 uint32_t time, uint32_t button, uint32_t state_w)
138 GstGLWindowWaylandEGL *window_egl = data;
139 guint edges = _get_closest_pointer_corner (window_egl);
140 window_egl->display.serial = serial;
142 if (button == BTN_LEFT && state_w == WL_POINTER_BUTTON_STATE_PRESSED)
143 wl_shell_surface_move (window_egl->window.wl_shell_surface,
144 window_egl->display.seat, serial);
146 if (button == BTN_RIGHT && state_w == WL_POINTER_BUTTON_STATE_PRESSED)
147 wl_shell_surface_resize (window_egl->window.wl_shell_surface,
148 window_egl->display.seat, serial, edges);
152 pointer_handle_axis (void *data, struct wl_pointer *pointer, uint32_t time,
153 uint32_t axis, wl_fixed_t value)
157 static const struct wl_pointer_listener pointer_listener = {
158 pointer_handle_enter,
159 pointer_handle_leave,
160 pointer_handle_motion,
161 pointer_handle_button,
166 seat_handle_capabilities (void *data, struct wl_seat *seat,
167 enum wl_seat_capability caps)
169 GstGLWindowWaylandEGL *window_egl = data;
170 struct display *display = &window_egl->display;
172 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !display->pointer) {
173 display->pointer = wl_seat_get_pointer (seat);
174 wl_pointer_set_user_data (display->pointer, window_egl);
175 wl_pointer_add_listener (display->pointer, &pointer_listener, window_egl);
176 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && display->pointer) {
177 wl_pointer_destroy (display->pointer);
178 display->pointer = NULL;
181 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
182 input->keyboard = wl_seat_get_keyboard (seat);
183 wl_keyboard_set_user_data (input->keyboard, input);
184 wl_keyboard_add_listener (input->keyboard, &keyboard_listener, input);
185 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
186 wl_keyboard_destroy (input->keyboard);
187 input->keyboard = NULL;
192 static const struct wl_seat_listener seat_listener = {
193 seat_handle_capabilities,
197 handle_ping (void *data, struct wl_shell_surface *wl_shell_surface,
200 GstGLWindowWaylandEGL *window_egl = data;
202 GST_TRACE_OBJECT (window_egl, "ping received serial %u", serial);
204 wl_shell_surface_pong (wl_shell_surface, serial);
207 static void window_resize (GstGLWindowWaylandEGL * window_egl, guint width,
211 handle_configure (void *data, struct wl_shell_surface *wl_shell_surface,
212 uint32_t edges, int32_t width, int32_t height)
214 GstGLWindowWaylandEGL *window_egl = data;
216 GST_DEBUG ("configure event on surface %p, %ix%i", wl_shell_surface, width,
219 window_resize (window_egl, width, height);
223 handle_popup_done (void *data, struct wl_shell_surface *wl_shell_surface)
227 static const struct wl_shell_surface_listener wl_shell_surface_listener = {
234 destroy_surfaces (GstGLWindowWaylandEGL * window_egl)
236 if (window_egl->window.subsurface) {
237 wl_subsurface_destroy (window_egl->window.subsurface);
238 window_egl->window.subsurface = NULL;
240 if (window_egl->window.wl_shell_surface) {
241 wl_shell_surface_destroy (window_egl->window.wl_shell_surface);
242 window_egl->window.wl_shell_surface = NULL;
244 if (window_egl->window.surface) {
245 wl_surface_destroy (window_egl->window.surface);
246 window_egl->window.surface = NULL;
248 if (window_egl->window.native) {
249 wl_egl_window_destroy (window_egl->window.native);
250 window_egl->window.native = NULL;
253 static struct wl_shell_surface *
254 create_wl_shell_surface (GstGLWindowWaylandEGL * window_egl,
255 GstGLDisplayWayland *display)
257 struct wl_shell_surface *wl_shell_surface;
259 wl_shell_surface = wl_shell_get_shell_surface (display->wl_shell,
260 window_egl->window.surface);
262 if (window_egl->window.queue) {
263 wl_proxy_set_queue ((struct wl_proxy *) wl_shell_surface,
264 window_egl->window.queue);
267 wl_shell_surface_add_listener (wl_shell_surface, &wl_shell_surface_listener,
269 wl_shell_surface_set_title (wl_shell_surface, "OpenGL Renderer");
270 wl_shell_surface_set_toplevel (wl_shell_surface);
272 return wl_shell_surface;
276 create_surfaces (GstGLWindowWaylandEGL * window_egl)
278 GstGLDisplayWayland *display =
279 GST_GL_DISPLAY_WAYLAND (GST_GL_WINDOW (window_egl)->display);
282 if (!window_egl->window.surface) {
283 window_egl->window.surface =
284 wl_compositor_create_surface (display->compositor);
285 if (window_egl->window.queue)
286 wl_proxy_set_queue ((struct wl_proxy *) window_egl->window.surface,
287 window_egl->window.queue);
290 if (window_egl->window.foreign_surface) {
292 if (!display->subcompositor) {
293 GST_ERROR_OBJECT (window_egl,
294 "Wayland server does not support subsurfaces");
295 window_egl->window.foreign_surface = NULL;
299 if (!window_egl->window.subsurface) {
300 window_egl->window.subsurface =
301 wl_subcompositor_get_subsurface (display->subcompositor,
302 window_egl->window.surface, window_egl->window.foreign_surface);
303 if (window_egl->window.queue)
304 wl_proxy_set_queue ((struct wl_proxy *) window_egl->window.subsurface,
305 window_egl->window.queue);
307 wl_subsurface_set_position (window_egl->window.subsurface,
308 window_egl->window.window_x, window_egl->window.window_y);
309 wl_subsurface_set_desync (window_egl->window.subsurface);
313 if (!window_egl->window.wl_shell_surface) {
314 window_egl->window.wl_shell_surface = create_wl_shell_surface (display);
319 * render_rect is the application requested size so choose that first if
321 * Else choose the already chosen size if set
322 * Else choose the preferred size if set
323 * Else choose a default value
325 if (window_egl->window.render_rect.w > 0)
326 width = window_egl->window.render_rect.w;
327 else if (window_egl->window.window_width > 0)
328 width = window_egl->window.window_width;
329 else if (window_egl->window.preferred_width > 0)
330 width = window_egl->window.preferred_width;
333 window_egl->window.window_width = width;
335 if (window_egl->window.render_rect.h > 0)
336 height = window_egl->window.render_rect.h;
337 else if (window_egl->window.window_height > 0)
338 height = window_egl->window.window_height;
339 else if (window_egl->window.preferred_height > 0)
340 height = window_egl->window.preferred_height;
343 window_egl->window.window_height = height;
345 if (!window_egl->window.native) {
346 gst_gl_window_resize (GST_GL_WINDOW (window_egl), width, height);
348 window_egl->window.native =
349 wl_egl_window_create (window_egl->window.surface, width, height);
350 if (window_egl->window.queue)
351 wl_proxy_set_queue ((struct wl_proxy *) window_egl->window.native,
352 window_egl->window.queue);
357 gst_gl_window_wayland_egl_class_init (GstGLWindowWaylandEGLClass * klass)
359 GstGLWindowClass *window_class = (GstGLWindowClass *) klass;
361 window_class->get_window_handle =
362 GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_get_window_handle);
363 window_class->set_window_handle =
364 GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_window_handle);
365 window_class->show = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_show);
366 window_class->draw = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_draw);
367 window_class->close = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_close);
368 window_class->open = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_open);
369 window_class->get_display =
370 GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_get_display);
371 window_class->set_render_rectangle =
372 GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_render_rectangle);
373 window_class->set_preferred_size =
374 GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_preferred_size);
378 gst_gl_window_wayland_egl_init (GstGLWindowWaylandEGL * window)
380 window->window.render_rect.w = window->window.render_rect.h = -1;
383 /* Must be called in the gl thread */
384 GstGLWindowWaylandEGL *
385 gst_gl_window_wayland_egl_new (GstGLDisplay * display)
387 GstGLWindowWaylandEGL *window;
389 if ((gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_WAYLAND)
391 /* we require a wayland display to create wayland surfaces */
394 GST_DEBUG ("creating Wayland EGL window");
396 window = g_object_new (GST_TYPE_GL_WINDOW_WAYLAND_EGL, NULL);
397 gst_object_ref_sink (window);
403 gst_gl_window_wayland_egl_close (GstGLWindow * window)
405 GstGLWindowWaylandEGL *window_egl;
407 window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
409 destroy_surfaces (window_egl);
411 g_source_destroy (window_egl->wl_source);
412 g_source_unref (window_egl->wl_source);
413 window_egl->wl_source = NULL;
415 GST_GL_WINDOW_CLASS (parent_class)->close (window);
419 gst_gl_window_wayland_egl_open (GstGLWindow * window, GError ** error)
421 GstGLDisplayWayland *display;
422 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
424 if (!GST_IS_GL_DISPLAY_WAYLAND (window->display)) {
425 g_set_error (error, GST_GL_WINDOW_ERROR,
426 GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
427 "Failed to retrieve Wayland display (wrong type?)");
430 display = GST_GL_DISPLAY_WAYLAND (window->display);
432 if (!display->display) {
433 g_set_error (error, GST_GL_WINDOW_ERROR,
434 GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
435 "Failed to retrieve Wayland display");
439 window_egl->window.queue = wl_display_create_queue (display->display);
441 window_egl->wl_source = wayland_event_source_new (display->display,
442 window_egl->window.queue);
444 if (!GST_GL_WINDOW_CLASS (parent_class)->open (window, error))
447 g_source_attach (window_egl->wl_source, window->main_context);
453 gst_gl_window_wayland_egl_create_window (GstGLWindowWaylandEGL * window_egl)
455 create_surfaces (window_egl);
459 gst_gl_window_wayland_egl_get_window_handle (GstGLWindow * window)
461 return (guintptr) GST_GL_WINDOW_WAYLAND_EGL (window)->window.native;
465 gst_gl_window_wayland_egl_set_window_handle (GstGLWindow * window,
468 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
469 struct wl_surface *surface = (struct wl_surface *) handle;
471 /* already set the NULL handle */
472 if (surface == NULL && window_egl->window.foreign_surface == NULL)
476 destroy_surfaces (window_egl);
477 window_egl->window.foreign_surface = surface;
478 create_surfaces (window_egl);
482 _roundtrip_async (GstGLWindow * window)
484 GstGLDisplayWayland *display_wayland =
485 GST_GL_DISPLAY_WAYLAND (window->display);
486 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
488 create_surfaces (window_egl);
490 if (gst_gl_wl_display_roundtrip_queue (display_wayland->display,
491 window_egl->window.queue) < 0)
492 GST_WARNING_OBJECT (window, "failed a roundtrip");
496 gst_gl_window_wayland_egl_show (GstGLWindow * window)
498 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
500 create_surfaces (window_egl);
502 gst_gl_window_send_message (window, (GstGLWindowCB) _roundtrip_async, window);
506 window_resize (GstGLWindowWaylandEGL * window_egl, guint width, guint height)
508 GstGLWindow *window = GST_GL_WINDOW (window_egl);
510 GST_DEBUG ("resizing window from %ux%u to %ux%u",
511 window_egl->window.window_width, window_egl->window.window_height, width,
514 if (window_egl->window.native) {
515 wl_egl_window_resize (window_egl->window.native, width, height, 0, 0);
518 gst_gl_window_resize (window, width, height);
520 window_egl->window.window_width = width;
521 window_egl->window.window_height = height;
525 draw_cb (gpointer data)
527 GstGLWindowWaylandEGL *window_egl = data;
528 GstGLWindow *window = GST_GL_WINDOW (window_egl);
529 GstGLContext *context = gst_gl_window_get_context (window);
531 create_surfaces (window_egl);
533 if (window_egl->window.subsurface)
534 wl_subsurface_set_desync (window_egl->window.subsurface);
536 if (window->queue_resize) {
539 gst_gl_window_get_surface_dimensions (window, &width, &height);
540 gst_gl_window_resize (window, width, height);
544 window->draw (window->draw_data);
546 gst_gl_context_swap_buffers (context);
548 if (window_egl->window.subsurface)
549 wl_subsurface_set_desync (window_egl->window.subsurface);
551 gst_object_unref (context);
555 gst_gl_window_wayland_egl_draw (GstGLWindow * window)
557 gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, window);
560 struct SetRenderRectangle
562 GstGLWindowWaylandEGL *window_egl;
563 GstVideoRectangle rect;
567 _free_set_render_rectangle (struct SetRenderRectangle *render)
570 if (render->window_egl)
571 gst_object_unref (render->window_egl);
577 _set_render_rectangle (gpointer data)
579 struct SetRenderRectangle *render = data;
581 GST_LOG_OBJECT (render->window_egl, "setting render rectangle %i,%i+%ix%i",
582 render->rect.x, render->rect.y, render->rect.w, render->rect.h);
584 if (render->window_egl->window.subsurface) {
585 wl_subsurface_set_sync (render->window_egl->window.subsurface);
586 wl_subsurface_set_position (render->window_egl->window.subsurface,
587 render->rect.x, render->rect.y);
588 render->window_egl->window.window_x = render->rect.x;
589 render->window_egl->window.window_y = render->rect.y;
592 window_resize (render->window_egl, render->rect.w, render->rect.h);
594 render->window_egl->window.render_rect = render->rect;
598 gst_gl_window_wayland_egl_set_render_rectangle (GstGLWindow * window,
599 gint x, gint y, gint width, gint height)
601 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
602 struct SetRenderRectangle *render;
604 render = g_new0 (struct SetRenderRectangle, 1);
605 render->window_egl = gst_object_ref (window_egl);
608 render->rect.w = width;
609 render->rect.h = height;
611 gst_gl_window_send_message_async (window,
612 (GstGLWindowCB) _set_render_rectangle, render,
613 (GDestroyNotify) _free_set_render_rectangle);
619 gst_gl_window_wayland_egl_set_preferred_size (GstGLWindow * window, gint width,
622 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
624 window_egl->window.preferred_width = width;
625 window_egl->window.preferred_height = height;
626 if (window_egl->window.render_rect.w < 0
627 && window_egl->window.render_rect.h < 0) {
628 if (window_egl->window.window_height != height
629 || window_egl->window.window_width != width) {
630 window_resize (window_egl, width, height);
636 gst_gl_window_wayland_egl_get_display (GstGLWindow * window)
638 GstGLDisplayWayland *display = GST_GL_DISPLAY_WAYLAND (window->display);
640 return (guintptr) display->display;