Debian Codegraph @

gst-libs/gst/gl/wayland/gstglwindow_wayland_egl.c

raw
1 /*
2 * GStreamer
3 * Copyright (C) 2008 Julien Isorce <[email protected]>
4 * Copyright (C) 2012 Matthew Waters <[email protected]>
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 if (g_get_prgname ()) {
309 xdg_toplevel_set_app_id (xdg_toplevel, g_get_prgname ());
310 } else {
311 xdg_toplevel_set_app_id (xdg_toplevel, "org.gstreamer.wayland");
312 }
313 xdg_toplevel_set_title (xdg_toplevel, "OpenGL Renderer");
314 xdg_toplevel_add_listener (xdg_toplevel, &xdg_toplevel_listener, window_egl);
315
316 /* Commit the xdg_surface state */
317 wl_surface_commit (window_egl->window.surface);
318
319 /* And save them into the fields */
320 window_egl->window.xdg_surface = xdg_surface;
321 window_egl->window.xdg_toplevel = xdg_toplevel;
322 }
323
324 static void
325 create_wl_shell_surface (GstGLWindowWaylandEGL * window_egl)
326 {
327 struct wl_shell_surface *wl_shell_surface;
328
329 GST_DEBUG ("Creating surfaces for wl-shell");
330
331 wl_shell_surface = wl_shell_get_shell_surface (window_egl->display.shell,
332 window_egl->window.surface);
333
334 wl_shell_surface_add_listener (wl_shell_surface, &wl_shell_surface_listener,
335 window_egl);
336 wl_shell_surface_set_title (wl_shell_surface, "OpenGL Renderer");
337 wl_shell_surface_set_toplevel (wl_shell_surface);
338
339 window_egl->window.wl_shell_surface = wl_shell_surface;
340 }
341
342 static void
343 create_surfaces (GstGLWindowWaylandEGL * window_egl)
344 {
345 gint width, height;
346
347 if (!window_egl->window.surface) {
348 window_egl->window.surface =
349 wl_compositor_create_surface (window_egl->display.compositor);
350 }
351
352 if (window_egl->window.foreign_surface) {
353 /* (re)parent */
354 if (!window_egl->display.subcompositor) {
355 GST_ERROR_OBJECT (window_egl,
356 "Wayland server does not support subsurfaces");
357 window_egl->window.foreign_surface = NULL;
358 goto shell_window;
359 }
360
361 if (!window_egl->window.subsurface) {
362 window_egl->window.subsurface =
363 wl_subcompositor_get_subsurface (window_egl->display.subcompositor,
364 window_egl->window.surface, window_egl->window.foreign_surface);
365
366 wl_subsurface_set_position (window_egl->window.subsurface,
367 window_egl->window.window_x, window_egl->window.window_y);
368 wl_subsurface_set_desync (window_egl->window.subsurface);
369 }
370 } else {
371 shell_window:
372 if (window_egl->display.xdg_wm_base) {
373 if (!window_egl->window.xdg_surface)
374 create_xdg_surface_and_toplevel (window_egl);
375 } else if (!window_egl->window.wl_shell_surface) {
376 create_wl_shell_surface (window_egl);
377 }
378 }
379
380 /*
381 * render_rect is the application requested size so choose that first if
382 * available.
383 * Else choose the already chosen size if set
384 * Else choose the preferred size if set
385 * Else choose a default value
386 */
387 if (window_egl->window.render_rect.w > 0)
388 width = window_egl->window.render_rect.w;
389 else if (window_egl->window.window_width > 0)
390 width = window_egl->window.window_width;
391 else if (window_egl->window.preferred_width > 0)
392 width = window_egl->window.preferred_width;
393 else
394 width = 320;
395 window_egl->window.window_width = width;
396
397 if (window_egl->window.render_rect.h > 0)
398 height = window_egl->window.render_rect.h;
399 else if (window_egl->window.window_height > 0)
400 height = window_egl->window.window_height;
401 else if (window_egl->window.preferred_height > 0)
402 height = window_egl->window.preferred_height;
403 else
404 height = 240;
405 window_egl->window.window_height = height;
406
407 if (!window_egl->window.native) {
408 gst_gl_window_resize (GST_GL_WINDOW (window_egl), width, height);
409
410 window_egl->window.native =
411 wl_egl_window_create (window_egl->window.surface, width, height);
412 }
413 }
414
415 static void
416 gst_gl_window_wayland_egl_class_init (GstGLWindowWaylandEGLClass * klass)
417 {
418 GstGLWindowClass *window_class = (GstGLWindowClass *) klass;
419
420 window_class->get_window_handle =
421 GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_get_window_handle);
422 window_class->set_window_handle =
423 GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_window_handle);
424 window_class->show = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_show);
425 window_class->draw = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_draw);
426 window_class->close = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_close);
427 window_class->open = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_open);
428 window_class->get_display =
429 GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_get_display);
430 window_class->set_render_rectangle =
431 GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_render_rectangle);
432 window_class->set_preferred_size =
433 GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_preferred_size);
434 }
435
436 static void
437 gst_gl_window_wayland_egl_init (GstGLWindowWaylandEGL * window)
438 {
439 window->window.render_rect.w = window->window.render_rect.h = -1;
440 }
441
442 /* Must be called in the gl thread */
443 GstGLWindowWaylandEGL *
444 gst_gl_window_wayland_egl_new (GstGLDisplay * display)
445 {
446 GstGLWindowWaylandEGL *window;
447
448 if ((gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_WAYLAND)
449 == 0)
450 /* we require a wayland display to create wayland surfaces */
451 return NULL;
452
453 GST_DEBUG ("creating Wayland EGL window");
454
455 window = g_object_new (GST_TYPE_GL_WINDOW_WAYLAND_EGL, NULL);
456 gst_object_ref_sink (window);
457
458 return window;
459 }
460
461 static void
462 gst_gl_window_wayland_egl_close (GstGLWindow * gl_window)
463 {
464 GstGLWindowWaylandEGL *window_egl;
465 struct display *display;
466
467 window_egl = GST_GL_WINDOW_WAYLAND_EGL (gl_window);
468 display = &window_egl->display;
469
470 if (display->pointer != NULL) {
471 wl_pointer_destroy (display->pointer);
472 display->pointer = NULL;
473 }
474
475 destroy_surfaces (window_egl);
476
477 /* As we are about to destroy the wl_source, we need to ensure everything
478 * has been sent synchronously, otherwise we will be leaking surfaces on
479 * server, leaving the window visible and unrefreshed on screen. */
480 wl_display_flush (GST_GL_DISPLAY_WAYLAND (gl_window->display)->display);
481
482 g_source_destroy (window_egl->wl_source);
483 g_source_unref (window_egl->wl_source);
484 window_egl->wl_source = NULL;
485
486 if (window_egl->display.registry)
487 wl_registry_destroy (window_egl->display.registry);
488
489 if (window_egl->display.compositor)
490 wl_compositor_destroy (window_egl->display.compositor);
491
492 if (window_egl->display.subcompositor)
493 wl_subcompositor_destroy (window_egl->display.subcompositor);
494
495 if (window_egl->display.xdg_wm_base)
496 xdg_wm_base_destroy (window_egl->display.xdg_wm_base);
497
498 if (window_egl->display.shell)
499 wl_shell_destroy (window_egl->display.shell);
500
501 if (window_egl->display.seat)
502 wl_seat_destroy (window_egl->display.seat);
503
504 wl_proxy_wrapper_destroy (window_egl->display.display);
505 wl_event_queue_destroy (window_egl->window.queue);
506
507 GST_GL_WINDOW_CLASS (parent_class)->close (gl_window);
508 }
509
510 static void
511 handle_xdg_wm_base_ping (void *user_data, struct xdg_wm_base *xdg_wm_base,
512 uint32_t serial)
513 {
514 xdg_wm_base_pong (xdg_wm_base, serial);
515 }
516
517 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
518 handle_xdg_wm_base_ping
519 };
520
521 static void
522 registry_handle_global (void *data, struct wl_registry *registry,
523 uint32_t name, const char *interface, uint32_t version)
524 {
525 GstGLWindowWaylandEGL *window_wayland = data;
526
527 GST_TRACE_OBJECT (window_wayland, "registry_handle_global with registry %p, "
528 "name %" G_GUINT32_FORMAT ", interface %s, version %u", registry, name,
529 interface, version);
530
531 if (g_strcmp0 (interface, "wl_compositor") == 0) {
532 window_wayland->display.compositor =
533 wl_registry_bind (registry, name, &wl_compositor_interface, 1);
534 } else if (g_strcmp0 (interface, "wl_subcompositor") == 0) {
535 window_wayland->display.subcompositor =
536 wl_registry_bind (registry, name, &wl_subcompositor_interface, 1);
537 } else if (g_strcmp0 (interface, "xdg_wm_base") == 0) {
538 window_wayland->display.xdg_wm_base =
539 wl_registry_bind (registry, name, &xdg_wm_base_interface, 1);
540 xdg_wm_base_add_listener (window_wayland->display.xdg_wm_base,
541 &xdg_wm_base_listener, window_wayland);
542 } else if (g_strcmp0 (interface, "wl_shell") == 0) {
543 window_wayland->display.shell =
544 wl_registry_bind (registry, name, &wl_shell_interface, 1);
545 } else if (g_strcmp0 (interface, "wl_seat") == 0) {
546 window_wayland->display.seat =
547 wl_registry_bind (registry, name, &wl_seat_interface, 4);
548 wl_seat_add_listener (window_wayland->display.seat, &seat_listener,
549 window_wayland);
550 }
551 }
552
553 static void
554 registry_handle_global_remove (void *data, struct wl_registry *registry,
555 uint32_t name)
556 {
557 GstGLWindowWaylandEGL *window_wayland = data;
558
559 /* TODO: deal with any registry objects that may be removed */
560 GST_TRACE_OBJECT (window_wayland, "wl_registry %p global_remove %"
561 G_GUINT32_FORMAT, registry, name);
562 }
563
564 static const struct wl_registry_listener registry_listener = {
565 registry_handle_global,
566 registry_handle_global_remove,
567 };
568
569 static gboolean
570 gst_gl_window_wayland_egl_open (GstGLWindow * window, GError ** error)
571 {
572 GstGLDisplayWayland *display;
573 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
574
575 if (!GST_IS_GL_DISPLAY_WAYLAND (window->display)) {
576 g_set_error (error, GST_GL_WINDOW_ERROR,
577 GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
578 "Failed to retrieve Wayland display (wrong type?)");
579 return FALSE;
580 }
581 display = GST_GL_DISPLAY_WAYLAND (window->display);
582
583 if (!display->display) {
584 g_set_error (error, GST_GL_WINDOW_ERROR,
585 GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
586 "Failed to retrieve Wayland display");
587 return FALSE;
588 }
589
590 /* we create a proxy wrapper for the display so that we can set the queue on
591 * it. This allows us to avoid sprinkling `wl_proxy_set_queue()` calls for
592 * each wayland resource we create as well as removing a race between
593 * creation and the `wl_proxy_set_queue()` call. */
594 window_egl->display.display = wl_proxy_create_wrapper (display->display);
595 window_egl->window.queue = wl_display_create_queue (display->display);
596
597 wl_proxy_set_queue ((struct wl_proxy *) window_egl->display.display,
598 window_egl->window.queue);
599
600 window_egl->display.registry =
601 wl_display_get_registry (window_egl->display.display);
602 wl_registry_add_listener (window_egl->display.registry, &registry_listener,
603 window_egl);
604
605 if (wl_display_roundtrip_queue (display->display,
606 window_egl->window.queue) < 0) {
607 g_set_error (error, GST_GL_WINDOW_ERROR,
608 GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
609 "Failed to perform a wayland roundtrip");
610 return FALSE;
611 }
612
613 window_egl->wl_source = wayland_event_source_new (display->display,
614 window_egl->window.queue);
615
616 if (!GST_GL_WINDOW_CLASS (parent_class)->open (window, error))
617 return FALSE;
618
619 g_source_attach (window_egl->wl_source, window->main_context);
620
621 return TRUE;
622 }
623
624 void
625 gst_gl_window_wayland_egl_create_window (GstGLWindowWaylandEGL * window_egl)
626 {
627 GstGLWindow *window = GST_GL_WINDOW (window_egl);
628
629 if (gst_gl_window_get_request_output_surface (window))
630 create_surfaces (window_egl);
631 }
632
633 static guintptr
634 gst_gl_window_wayland_egl_get_window_handle (GstGLWindow * window)
635 {
636 return (guintptr) GST_GL_WINDOW_WAYLAND_EGL (window)->window.native;
637 }
638
639 static void
640 gst_gl_window_wayland_egl_set_window_handle (GstGLWindow * window,
641 guintptr handle)
642 {
643 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
644 struct wl_surface *surface = (struct wl_surface *) handle;
645
646 /* already set the NULL handle */
647 if (surface == NULL && window_egl->window.foreign_surface == NULL)
648 return;
649
650 /* unparent */
651 destroy_surfaces (window_egl);
652 window_egl->window.foreign_surface = surface;
653 create_surfaces (window_egl);
654 }
655
656 static void
657 _roundtrip_async (GstGLWindow * window)
658 {
659 GstGLDisplayWayland *display = GST_GL_DISPLAY_WAYLAND (window->display);
660 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
661
662 create_surfaces (window_egl);
663
664 if (wl_display_roundtrip_queue (display->display,
665 window_egl->window.queue) < 0)
666 GST_WARNING_OBJECT (window, "failed a roundtrip");
667 }
668
669 static void
670 gst_gl_window_wayland_egl_show (GstGLWindow * window)
671 {
672 gst_gl_window_send_message (window, (GstGLWindowCB) _roundtrip_async, window);
673 }
674
675 static void
676 window_resize (GstGLWindowWaylandEGL * window_egl, guint width, guint height)
677 {
678 GstGLWindow *window = GST_GL_WINDOW (window_egl);
679
680 GST_DEBUG ("resizing window from %ux%u to %ux%u",
681 window_egl->window.window_width, window_egl->window.window_height, width,
682 height);
683
684 if (window_egl->window.native) {
685 wl_egl_window_resize (window_egl->window.native, width, height, 0, 0);
686 }
687
688 gst_gl_window_resize (window, width, height);
689
690 window_egl->window.window_width = width;
691 window_egl->window.window_height = height;
692 }
693
694 static void
695 draw_cb (gpointer data)
696 {
697 GstGLWindowWaylandEGL *window_egl = data;
698 GstGLWindow *window = GST_GL_WINDOW (window_egl);
699 GstGLContext *context = gst_gl_window_get_context (window);
700
701 create_surfaces (window_egl);
702
703 if (window_egl->window.subsurface)
704 wl_subsurface_set_desync (window_egl->window.subsurface);
705
706 if (window->queue_resize) {
707 guint width, height;
708
709 gst_gl_window_get_surface_dimensions (window, &width, &height);
710 gst_gl_window_resize (window, width, height);
711 }
712
713 if (window->draw)
714 window->draw (window->draw_data);
715
716 gst_gl_context_swap_buffers (context);
717
718 if (window_egl->window.subsurface)
719 wl_subsurface_set_desync (window_egl->window.subsurface);
720
721 gst_object_unref (context);
722 }
723
724 static void
725 gst_gl_window_wayland_egl_draw (GstGLWindow * window)
726 {
727 gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, window);
728 }
729
730 struct SetRenderRectangle
731 {
732 GstGLWindowWaylandEGL *window_egl;
733 GstVideoRectangle rect;
734 };
735
736 static void
737 _free_set_render_rectangle (struct SetRenderRectangle *render)
738 {
739 if (render) {
740 if (render->window_egl)
741 gst_object_unref (render->window_egl);
742 g_free (render);
743 }
744 }
745
746 static void
747 _set_render_rectangle (gpointer data)
748 {
749 struct SetRenderRectangle *render = data;
750
751 GST_LOG_OBJECT (render->window_egl, "setting render rectangle %i,%i+%ix%i",
752 render->rect.x, render->rect.y, render->rect.w, render->rect.h);
753
754 if (render->window_egl->window.subsurface) {
755 wl_subsurface_set_sync (render->window_egl->window.subsurface);
756 wl_subsurface_set_position (render->window_egl->window.subsurface,
757 render->rect.x, render->rect.y);
758 render->window_egl->window.window_x = render->rect.x;
759 render->window_egl->window.window_y = render->rect.y;
760 }
761
762 window_resize (render->window_egl, render->rect.w, render->rect.h);
763
764 render->window_egl->window.render_rect = render->rect;
765 }
766
767 static gboolean
768 gst_gl_window_wayland_egl_set_render_rectangle (GstGLWindow * window,
769 gint x, gint y, gint width, gint height)
770 {
771 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
772 struct SetRenderRectangle *render;
773
774 render = g_new0 (struct SetRenderRectangle, 1);
775 render->window_egl = gst_object_ref (window_egl);
776 render->rect.x = x;
777 render->rect.y = y;
778 render->rect.w = width;
779 render->rect.h = height;
780
781 gst_gl_window_send_message_async (window,
782 (GstGLWindowCB) _set_render_rectangle, render,
783 (GDestroyNotify) _free_set_render_rectangle);
784
785 return TRUE;
786 }
787
788 static void
789 gst_gl_window_wayland_egl_set_preferred_size (GstGLWindow * window, gint width,
790 gint height)
791 {
792 GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
793
794 window_egl->window.preferred_width = width;
795 window_egl->window.preferred_height = height;
796 if (window_egl->window.render_rect.w < 0
797 && window_egl->window.render_rect.h < 0) {
798 if (window_egl->window.window_height != height
799 || window_egl->window.window_width != width) {
800 window_resize (window_egl, width, height);
801 }
802 }
803 }
804
805 static guintptr
806 gst_gl_window_wayland_egl_get_display (GstGLWindow * window)
807 {
808 GstGLDisplayWayland *display = GST_GL_DISPLAY_WAYLAND (window->display);
809
810 return (guintptr) display->display;
811 }