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 const gchar *WlEGLErrorString ();
40 #define GST_CAT_DEFAULT gst_gl_window_debug
42 #define gst_gl_window_wayland_egl_parent_class parent_class
43 G_DEFINE_TYPE (GstGLWindowWaylandEGL, gst_gl_window_wayland_egl,
46 static guintptr gst_gl_window_wayland_egl_get_window_handle (GstGLWindow *
48 static void gst_gl_window_wayland_egl_set_window_handle (GstGLWindow * window,
50 static void gst_gl_window_wayland_egl_show (GstGLWindow * window);
51 static void gst_gl_window_wayland_egl_draw (GstGLWindow * window);
52 static void gst_gl_window_wayland_egl_close (GstGLWindow * window);
53 static gboolean gst_gl_window_wayland_egl_open (GstGLWindow * window,
55 static guintptr gst_gl_window_wayland_egl_get_display (GstGLWindow * window);
56 static gboolean gst_gl_window_wayland_egl_set_render_rectangle (GstGLWindow *
57 window, gint x, gint y, gint width, gint height);
61 pointer_handle_enter (void *data, struct wl_pointer *pointer, uint32_t serial,
62 struct wl_surface *surface, wl_fixed_t sx_w, wl_fixed_t sy_w)
64 GstGLWindowWaylandEGL *window_egl = data;
65 struct wl_buffer *buffer;
66 struct wl_cursor_image *image = NULL;
68 window_egl->display.serial = serial;
70 if (window_egl->display.default_cursor) {
71 image = window_egl->display.default_cursor->images[0];
72 buffer = wl_cursor_image_get_buffer (image);
73 wl_pointer_set_cursor (pointer, serial,
74 window_egl->display.cursor_surface, image->hotspot_x, image->hotspot_y);
75 wl_surface_attach (window_egl->display.cursor_surface, buffer, 0, 0);
76 wl_surface_damage (window_egl->display.cursor_surface, 0, 0,
77 image->width, image->height);
78 wl_surface_commit (window_egl->display.cursor_surface);
83 pointer_handle_leave (void *data, struct wl_pointer *pointer, uint32_t serial,
84 struct wl_surface *surface)
86 GstGLWindowWaylandEGL *window_egl = data;
87 window_egl->display.serial = serial;
91 pointer_handle_motion (void *data, struct wl_pointer *pointer, uint32_t time,
92 wl_fixed_t sx_w, wl_fixed_t sy_w)
94 GstGLWindowWaylandEGL *window_egl = data;
96 window_egl->display.pointer_x = wl_fixed_to_double (sx_w);
97 window_egl->display.pointer_y = wl_fixed_to_double (sy_w);
110 _get_closest_pointer_corner (GstGLWindowWaylandEGL * window_egl)
113 gdouble win_width, win_height;
116 win_width = (gdouble) window_egl->window.window_width;
117 win_height = (gdouble) window_egl->window.window_height;
118 p_x = window_egl->display.pointer_x;
119 p_y = window_egl->display.pointer_y;
121 if (win_width == 0.0 || win_height == 0.0)
124 edges |= win_width / 2.0 - p_x < 0.0 ? WL_EDGE_RIGHT : WL_EDGE_LEFT;
125 edges |= win_height / 2.0 - p_y < 0.0 ? WL_EDGE_BOTTOM : WL_EDGE_TOP;
131 pointer_handle_button (void *data, struct wl_pointer *pointer, uint32_t serial,
132 uint32_t time, uint32_t button, uint32_t state_w)
134 GstGLWindowWaylandEGL *window_egl = data;
135 guint edges = _get_closest_pointer_corner (window_egl);
136 window_egl->display.serial = serial;
138 if (button == BTN_LEFT && state_w == WL_POINTER_BUTTON_STATE_PRESSED)
139 wl_shell_surface_move (window_egl->window.shell_surface,
140 window_egl->display.seat, serial);
142 if (button == BTN_RIGHT && state_w == WL_POINTER_BUTTON_STATE_PRESSED)
143 wl_shell_surface_resize (window_egl->window.shell_surface,
144 window_egl->display.seat, serial, edges);
148 pointer_handle_axis (void *data, struct wl_pointer *pointer, uint32_t time,
149 uint32_t axis, wl_fixed_t value)
153 static const struct wl_pointer_listener pointer_listener = {
154 pointer_handle_enter,
155 pointer_handle_leave,
156 pointer_handle_motion,
157 pointer_handle_button,
162 seat_handle_capabilities (void *data, struct wl_seat *seat,
163 enum wl_seat_capability caps)
165 GstGLWindowWaylandEGL *window_egl = data;
166 struct display *display = &window_egl->display;
168 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !display->pointer) {
169 display->pointer = wl_seat_get_pointer (seat);
170 wl_pointer_set_user_data (display->pointer, window_egl);
171 wl_pointer_add_listener (display->pointer, &pointer_listener, window_egl);
172 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && display->pointer) {
173 wl_pointer_destroy (display->pointer);
174 display->pointer = NULL;
177 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
178 input->keyboard = wl_seat_get_keyboard (seat);
179 wl_keyboard_set_user_data (input->keyboard, input);
180 wl_keyboard_add_listener (input->keyboard, &keyboard_listener, input);
181 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
182 wl_keyboard_destroy (input->keyboard);
183 input->keyboard = NULL;
188 static const struct wl_seat_listener seat_listener = {
189 seat_handle_capabilities,
193 handle_ping (void *data, struct wl_shell_surface *shell_surface,
196 GstGLWindowWaylandEGL *window_egl = data;
198 GST_TRACE_OBJECT (window_egl, "ping received serial %u", serial);
200 wl_shell_surface_pong (shell_surface, serial);
203 static void window_resize (GstGLWindowWaylandEGL * window_egl, guint width,
207 handle_configure (void *data, struct wl_shell_surface *shell_surface,
208 uint32_t edges, int32_t width, int32_t height)
210 GstGLWindowWaylandEGL *window_egl = data;
212 GST_DEBUG ("configure event on surface %p, %ix%i", shell_surface, width,
215 window_resize (window_egl, width, height);
219 handle_popup_done (void *data, struct wl_shell_surface *shell_surface)
223 static const struct wl_shell_surface_listener shell_surface_listener = {
230 destroy_surfaces (GstGLWindowWaylandEGL * window_egl)
232 if (window_egl->window.subsurface) {
233 wl_subsurface_destroy (window_egl->window.subsurface);
234 window_egl->window.subsurface = NULL;
236 if (window_egl->window.shell_surface) {
237 wl_shell_surface_destroy (window_egl->window.shell_surface);
238 window_egl->window.shell_surface = NULL;
240 if (window_egl->window.surface) {
241 wl_surface_destroy (window_egl->window.surface);
242 window_egl->window.surface = NULL;
244 if (window_egl->window.native) {
245 wl_egl_window_destroy (window_egl->window.native);
246 window_egl->window.native = NULL;
251 create_surfaces (GstGLWindowWaylandEGL * window_egl)
253 GstGLDisplayWayland *display =
254 GST_GL_DISPLAY_WAYLAND (GST_GL_WINDOW (window_egl)->display);
257 if (!window_egl->window.surface) {
258 window_egl->window.surface =
259 wl_compositor_create_surface (display->compositor);
260 if (window_egl->window.queue)
261 wl_proxy_set_queue ((struct wl_proxy *) window_egl->window.surface,
262 window_egl->window.queue);
265 if (window_egl->window.foreign_surface) {
267 if (!display->subcompositor) {
268 GST_ERROR_OBJECT (window_egl,
269 "Wayland server does not support subsurfaces");
270 window_egl->window.foreign_surface = NULL;
274 if (!window_egl->window.subsurface) {
275 window_egl->window.subsurface =
276 wl_subcompositor_get_subsurface (display->subcompositor,
277 window_egl->window.surface, window_egl->window.foreign_surface);
278 if (window_egl->window.queue)
279 wl_proxy_set_queue ((struct wl_proxy *) window_egl->window.subsurface,
280 window_egl->window.queue);
282 wl_subsurface_set_position (window_egl->window.subsurface,
283 window_egl->window.window_x, window_egl->window.window_y);
284 wl_subsurface_set_desync (window_egl->window.subsurface);
288 if (!window_egl->window.shell_surface) {
289 window_egl->window.shell_surface =
290 wl_shell_get_shell_surface (display->shell,
291 window_egl->window.surface);
292 if (window_egl->window.queue)
293 wl_proxy_set_queue ((struct wl_proxy *) window_egl->window.
294 shell_surface, window_egl->window.queue);
296 wl_shell_surface_add_listener (window_egl->window.shell_surface,
297 &shell_surface_listener, window_egl);
299 wl_shell_surface_set_title (window_egl->window.shell_surface,
301 wl_shell_surface_set_toplevel (window_egl->window.shell_surface);
305 if (window_egl->window.window_width > 0)
306 width = window_egl->window.window_width;
309 window_egl->window.window_width = width;
311 if (window_egl->window.window_height > 0)
312 height = window_egl->window.window_height;
315 window_egl->window.window_height = height;
317 if (!window_egl->window.native) {
318 window_egl->window.native =
319 wl_egl_window_create (window_egl->window.surface, width, height);
320 if (window_egl->window.queue)
321 wl_proxy_set_queue ((struct wl_proxy *) window_egl->window.native,
322 window_egl->window.queue);
327 gst_gl_window_wayland_egl_class_init (GstGLWindowWaylandEGLClass * klass)
329 GstGLWindowClass *window_class = (GstGLWindowClass *) klass;
331 window_class->get_window_handle =
332 GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_get_window_handle);
333 window_class->set_window_handle =
334 GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_window_handle);
335 window_class->draw_unlocked =
336 GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_draw);
337 window_class->show = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_show);
338 window_class->draw = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_draw);
339 window_class->close = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_close);
340 window_class->open = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_open);
341 window_class->get_display =
342 GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_get_display);
343 window_class->set_render_rectangle =
344 GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_render_rectangle);
348 gst_gl_window_wayland_egl_init (GstGLWindowWaylandEGL * window)
352 /* Must be called in the gl thread */
353 GstGLWindowWaylandEGL *
354 gst_gl_window_wayland_egl_new (GstGLDisplay * display)
356 if ((gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_WAYLAND)
358 /* we require a wayland display to create wayland surfaces */
361 GST_DEBUG ("creating Wayland EGL window");
363 return g_object_new (GST_GL_TYPE_WINDOW_WAYLAND_EGL, NULL);
367 gst_gl_window_wayland_egl_close (GstGLWindow * window)
369 GstGLWindowWaylandEGL *window_egl;
371 window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
373 destroy_surfaces (window_egl);
375 g_source_destroy (window_egl->wl_source);
376 g_source_unref (window_egl->wl_source);
377 window_egl->wl_source = NULL;
379 GST_GL_WINDOW_CLASS (parent_class)->close (window);
383 gst_gl_window_wayland_egl_open (GstGLWindow * window, GError ** error)
385 GstGLDisplayWayland *display = GST_GL_DISPLAY_WAYLAND (window->display);
386 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
388 if (!display->display) {
389 g_set_error (error, GST_GL_WINDOW_ERROR,
390 GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
391 "Failed to retrieve Wayland display");
395 window_egl->window.queue = wl_display_create_queue (display->display);
397 window_egl->wl_source = wayland_event_source_new (display->display,
398 window_egl->window.queue);
400 if (!GST_GL_WINDOW_CLASS (parent_class)->open (window, error))
403 g_source_attach (window_egl->wl_source, g_main_context_get_thread_default ());
409 gst_gl_window_wayland_egl_create_window (GstGLWindowWaylandEGL * window_egl)
411 create_surfaces (window_egl);
415 gst_gl_window_wayland_egl_get_window_handle (GstGLWindow * window)
417 return (guintptr) GST_GL_WINDOW_WAYLAND_EGL (window)->window.native;
421 gst_gl_window_wayland_egl_set_window_handle (GstGLWindow * window,
424 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
425 struct wl_surface *surface = (struct wl_surface *) handle;
427 /* already set the NULL handle */
428 if (surface == NULL && window_egl->window.foreign_surface == NULL)
432 destroy_surfaces (window_egl);
433 window_egl->window.foreign_surface = surface;
434 create_surfaces (window_egl);
438 gst_gl_window_wayland_egl_show (GstGLWindow * window)
440 GstGLDisplayWayland *display_wayland =
441 GST_GL_DISPLAY_WAYLAND (window->display);
442 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
444 create_surfaces (window_egl);
446 if (gst_gl_wl_display_roundtrip_queue (display_wayland->display,
447 window_egl->window.queue) < 0)
448 GST_WARNING_OBJECT (window, "failed a roundtrip");
452 window_resize (GstGLWindowWaylandEGL * window_egl, guint width, guint height)
454 GstGLWindow *window = GST_GL_WINDOW (window_egl);
456 GST_DEBUG ("resizing window from %ux%u to %ux%u",
457 window_egl->window.window_width, window_egl->window.window_height, width,
460 if (window_egl->window.native) {
461 wl_egl_window_resize (window_egl->window.native, width, height, 0, 0);
465 window->resize (window->resize_data, width, height);
467 window_egl->window.window_width = width;
468 window_egl->window.window_height = height;
472 draw_cb (gpointer data)
474 GstGLWindowWaylandEGL *window_egl = data;
475 GstGLWindow *window = GST_GL_WINDOW (window_egl);
476 GstGLContext *context = gst_gl_window_get_context (window);
477 GstGLContextClass *context_class = GST_GL_CONTEXT_GET_CLASS (context);
479 create_surfaces (window_egl);
481 if (window_egl->window.subsurface)
482 wl_subsurface_set_desync (window_egl->window.subsurface);
485 window->draw (window->draw_data);
487 context_class->swap_buffers (context);
489 if (window_egl->window.subsurface)
490 wl_subsurface_set_desync (window_egl->window.subsurface);
492 gst_object_unref (context);
496 gst_gl_window_wayland_egl_draw (GstGLWindow * window)
498 gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, window);
501 struct SetRenderRectangle
503 GstGLWindowWaylandEGL *window_egl;
504 GstVideoRectangle rect;
508 _free_set_render_rectangle (struct SetRenderRectangle *render)
511 if (render->window_egl)
512 gst_object_unref (render->window_egl);
518 _set_render_rectangle (gpointer data)
520 struct SetRenderRectangle *render = data;
522 GST_LOG_OBJECT (render->window_egl, "setting render rectangle %i,%i+%ix%i",
523 render->rect.x, render->rect.y, render->rect.w, render->rect.h);
525 if (render->window_egl->window.subsurface) {
526 wl_subsurface_set_sync (render->window_egl->window.subsurface);
527 wl_subsurface_set_position (render->window_egl->window.subsurface,
528 render->rect.x, render->rect.y);
529 render->window_egl->window.window_x = render->rect.x;
530 render->window_egl->window.window_y = render->rect.y;
533 window_resize (render->window_egl, render->rect.w, render->rect.h);
537 gst_gl_window_wayland_egl_set_render_rectangle (GstGLWindow * window,
538 gint x, gint y, gint width, gint height)
540 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
541 struct SetRenderRectangle *render;
543 render = g_new0 (struct SetRenderRectangle, 1);
544 render->window_egl = gst_object_ref (window_egl);
547 render->rect.w = width;
548 render->rect.h = height;
550 gst_gl_window_send_message_async (window,
551 (GstGLWindowCB) _set_render_rectangle, render,
552 (GDestroyNotify) _free_set_render_rectangle);
558 gst_gl_window_wayland_egl_get_display (GstGLWindow * window)
560 GstGLDisplayWayland *display = GST_GL_DISPLAY_WAYLAND (window->display);
562 return (guintptr) display->display;