gl/wayland: extract code to create wl_shell_surface
[platform/upstream/gstreamer.git] / gst-libs / gst / gl / wayland / gstglwindow_wayland_egl.c
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 "gstglwindow_wayland_egl.h"
37
38 #include "../gstglwindow_private.h"
39
40 const gchar *WlEGLErrorString ();
41
42 #define GST_CAT_DEFAULT gst_gl_window_debug
43
44 #define gst_gl_window_wayland_egl_parent_class parent_class
45 G_DEFINE_TYPE (GstGLWindowWaylandEGL, gst_gl_window_wayland_egl,
46     GST_TYPE_GL_WINDOW);
47
48 static guintptr gst_gl_window_wayland_egl_get_window_handle (GstGLWindow *
49     window);
50 static void gst_gl_window_wayland_egl_set_window_handle (GstGLWindow * window,
51     guintptr handle);
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,
56     GError ** error);
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);
62
63 #if 0
64 static void
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)
67 {
68   GstGLWindowWaylandEGL *window_egl = data;
69   struct wl_buffer *buffer;
70   struct wl_cursor_image *image = NULL;
71
72   window_egl->display.serial = serial;
73
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);
83   }
84 }
85
86 static void
87 pointer_handle_leave (void *data, struct wl_pointer *pointer, uint32_t serial,
88     struct wl_surface *surface)
89 {
90   GstGLWindowWaylandEGL *window_egl = data;
91   window_egl->display.serial = serial;
92 }
93
94 static void
95 pointer_handle_motion (void *data, struct wl_pointer *pointer, uint32_t time,
96     wl_fixed_t sx_w, wl_fixed_t sy_w)
97 {
98   GstGLWindowWaylandEGL *window_egl = data;
99
100   window_egl->display.pointer_x = wl_fixed_to_double (sx_w);
101   window_egl->display.pointer_y = wl_fixed_to_double (sy_w);
102 }
103
104 enum wl_edges
105 {
106   WL_EDGE_NONE = 0,
107   WL_EDGE_TOP = 1,
108   WL_EDGE_BOTTOM = 2,
109   WL_EDGE_LEFT = 4,
110   WL_EDGE_RIGHT = 8,
111 };
112
113 static guint
114 _get_closest_pointer_corner (GstGLWindowWaylandEGL * window_egl)
115 {
116   guint edges = 0;
117   gdouble win_width, win_height;
118   gdouble p_x, p_y;
119
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;
124
125   if (win_width == 0.0 || win_height == 0.0)
126     return WL_EDGE_NONE;
127
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;
130
131   return edges;
132 }
133
134 static void
135 pointer_handle_button (void *data, struct wl_pointer *pointer, uint32_t serial,
136     uint32_t time, uint32_t button, uint32_t state_w)
137 {
138   GstGLWindowWaylandEGL *window_egl = data;
139   guint edges = _get_closest_pointer_corner (window_egl);
140   window_egl->display.serial = serial;
141
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);
145
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);
149 }
150
151 static void
152 pointer_handle_axis (void *data, struct wl_pointer *pointer, uint32_t time,
153     uint32_t axis, wl_fixed_t value)
154 {
155 }
156
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,
162   pointer_handle_axis,
163 };
164
165 static void
166 seat_handle_capabilities (void *data, struct wl_seat *seat,
167     enum wl_seat_capability caps)
168 {
169   GstGLWindowWaylandEGL *window_egl = data;
170   struct display *display = &window_egl->display;
171
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;
179   }
180 #if 0
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;
188   }
189 #endif
190 }
191
192 static const struct wl_seat_listener seat_listener = {
193   seat_handle_capabilities,
194 };
195 #endif
196 static void
197 handle_ping (void *data, struct wl_shell_surface *wl_shell_surface,
198     uint32_t serial)
199 {
200   GstGLWindowWaylandEGL *window_egl = data;
201
202   GST_TRACE_OBJECT (window_egl, "ping received serial %u", serial);
203
204   wl_shell_surface_pong (wl_shell_surface, serial);
205 }
206
207 static void window_resize (GstGLWindowWaylandEGL * window_egl, guint width,
208     guint height);
209
210 static void
211 handle_configure (void *data, struct wl_shell_surface *wl_shell_surface,
212     uint32_t edges, int32_t width, int32_t height)
213 {
214   GstGLWindowWaylandEGL *window_egl = data;
215
216   GST_DEBUG ("configure event on surface %p, %ix%i", wl_shell_surface, width,
217       height);
218
219   window_resize (window_egl, width, height);
220 }
221
222 static void
223 handle_popup_done (void *data, struct wl_shell_surface *wl_shell_surface)
224 {
225 }
226
227 static const struct wl_shell_surface_listener wl_shell_surface_listener = {
228   handle_ping,
229   handle_configure,
230   handle_popup_done
231 };
232
233 static void
234 destroy_surfaces (GstGLWindowWaylandEGL * window_egl)
235 {
236   if (window_egl->window.subsurface) {
237     wl_subsurface_destroy (window_egl->window.subsurface);
238     window_egl->window.subsurface = NULL;
239   }
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;
243   }
244   if (window_egl->window.surface) {
245     wl_surface_destroy (window_egl->window.surface);
246     window_egl->window.surface = NULL;
247   }
248   if (window_egl->window.native) {
249     wl_egl_window_destroy (window_egl->window.native);
250     window_egl->window.native = NULL;
251 }
252
253 static struct wl_shell_surface *
254 create_wl_shell_surface (GstGLWindowWaylandEGL * window_egl,
255     GstGLDisplayWayland *display)
256 {
257   struct wl_shell_surface *wl_shell_surface;
258
259   wl_shell_surface = wl_shell_get_shell_surface (display->wl_shell,
260       window_egl->window.surface);
261
262   if (window_egl->window.queue) {
263     wl_proxy_set_queue ((struct wl_proxy *) wl_shell_surface,
264         window_egl->window.queue);
265   }
266
267   wl_shell_surface_add_listener (wl_shell_surface, &wl_shell_surface_listener,
268     window_egl);
269   wl_shell_surface_set_title (wl_shell_surface, "OpenGL Renderer");
270   wl_shell_surface_set_toplevel (wl_shell_surface);
271
272   return wl_shell_surface;
273 }
274
275 static void
276 create_surfaces (GstGLWindowWaylandEGL * window_egl)
277 {
278   GstGLDisplayWayland *display =
279       GST_GL_DISPLAY_WAYLAND (GST_GL_WINDOW (window_egl)->display);
280   gint width, height;
281
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);
288   }
289
290   if (window_egl->window.foreign_surface) {
291     /* (re)parent */
292     if (!display->subcompositor) {
293       GST_ERROR_OBJECT (window_egl,
294           "Wayland server does not support subsurfaces");
295       window_egl->window.foreign_surface = NULL;
296       goto shell_window;
297     }
298
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);
306
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);
310     }
311   } else {
312   shell_window:
313     if (!window_egl->window.wl_shell_surface) {
314       window_egl->window.wl_shell_surface = create_wl_shell_surface (display);
315     }
316   }
317
318   /*
319    * render_rect is the application requested size so choose that first if
320    * available.
321    * Else choose the already chosen size if set
322    * Else choose the preferred size if set
323    * Else choose a default value
324    */
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;
331   else
332     width = 320;
333   window_egl->window.window_width = width;
334
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;
341   else
342     height = 240;
343   window_egl->window.window_height = height;
344
345   if (!window_egl->window.native) {
346     gst_gl_window_resize (GST_GL_WINDOW (window_egl), width, height);
347
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);
353   }
354 }
355
356 static void
357 gst_gl_window_wayland_egl_class_init (GstGLWindowWaylandEGLClass * klass)
358 {
359   GstGLWindowClass *window_class = (GstGLWindowClass *) klass;
360
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);
375 }
376
377 static void
378 gst_gl_window_wayland_egl_init (GstGLWindowWaylandEGL * window)
379 {
380   window->window.render_rect.w = window->window.render_rect.h = -1;
381 }
382
383 /* Must be called in the gl thread */
384 GstGLWindowWaylandEGL *
385 gst_gl_window_wayland_egl_new (GstGLDisplay * display)
386 {
387   GstGLWindowWaylandEGL *window;
388
389   if ((gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_WAYLAND)
390       == 0)
391     /* we require a wayland display to create wayland surfaces */
392     return NULL;
393
394   GST_DEBUG ("creating Wayland EGL window");
395
396   window = g_object_new (GST_TYPE_GL_WINDOW_WAYLAND_EGL, NULL);
397   gst_object_ref_sink (window);
398
399   return window;
400 }
401
402 static void
403 gst_gl_window_wayland_egl_close (GstGLWindow * window)
404 {
405   GstGLWindowWaylandEGL *window_egl;
406
407   window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
408
409   destroy_surfaces (window_egl);
410
411   g_source_destroy (window_egl->wl_source);
412   g_source_unref (window_egl->wl_source);
413   window_egl->wl_source = NULL;
414
415   GST_GL_WINDOW_CLASS (parent_class)->close (window);
416 }
417
418 static gboolean
419 gst_gl_window_wayland_egl_open (GstGLWindow * window, GError ** error)
420 {
421   GstGLDisplayWayland *display;
422   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
423
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?)");
428     return FALSE;
429   }
430   display = GST_GL_DISPLAY_WAYLAND (window->display);
431
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");
436     return FALSE;
437   }
438
439   window_egl->window.queue = wl_display_create_queue (display->display);
440
441   window_egl->wl_source = wayland_event_source_new (display->display,
442       window_egl->window.queue);
443
444   if (!GST_GL_WINDOW_CLASS (parent_class)->open (window, error))
445     return FALSE;
446
447   g_source_attach (window_egl->wl_source, window->main_context);
448
449   return TRUE;
450 }
451
452 void
453 gst_gl_window_wayland_egl_create_window (GstGLWindowWaylandEGL * window_egl)
454 {
455   create_surfaces (window_egl);
456 }
457
458 static guintptr
459 gst_gl_window_wayland_egl_get_window_handle (GstGLWindow * window)
460 {
461   return (guintptr) GST_GL_WINDOW_WAYLAND_EGL (window)->window.native;
462 }
463
464 static void
465 gst_gl_window_wayland_egl_set_window_handle (GstGLWindow * window,
466     guintptr handle)
467 {
468   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
469   struct wl_surface *surface = (struct wl_surface *) handle;
470
471   /* already set the NULL handle */
472   if (surface == NULL && window_egl->window.foreign_surface == NULL)
473     return;
474
475   /* unparent */
476   destroy_surfaces (window_egl);
477   window_egl->window.foreign_surface = surface;
478   create_surfaces (window_egl);
479 }
480
481 static void
482 _roundtrip_async (GstGLWindow * window)
483 {
484   GstGLDisplayWayland *display_wayland =
485       GST_GL_DISPLAY_WAYLAND (window->display);
486   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
487
488   create_surfaces (window_egl);
489
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");
493 }
494
495 static void
496 gst_gl_window_wayland_egl_show (GstGLWindow * window)
497 {
498   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
499
500   create_surfaces (window_egl);
501
502   gst_gl_window_send_message (window, (GstGLWindowCB) _roundtrip_async, window);
503 }
504
505 static void
506 window_resize (GstGLWindowWaylandEGL * window_egl, guint width, guint height)
507 {
508   GstGLWindow *window = GST_GL_WINDOW (window_egl);
509
510   GST_DEBUG ("resizing window from %ux%u to %ux%u",
511       window_egl->window.window_width, window_egl->window.window_height, width,
512       height);
513
514   if (window_egl->window.native) {
515     wl_egl_window_resize (window_egl->window.native, width, height, 0, 0);
516   }
517
518   gst_gl_window_resize (window, width, height);
519
520   window_egl->window.window_width = width;
521   window_egl->window.window_height = height;
522 }
523
524 static void
525 draw_cb (gpointer data)
526 {
527   GstGLWindowWaylandEGL *window_egl = data;
528   GstGLWindow *window = GST_GL_WINDOW (window_egl);
529   GstGLContext *context = gst_gl_window_get_context (window);
530
531   create_surfaces (window_egl);
532
533   if (window_egl->window.subsurface)
534     wl_subsurface_set_desync (window_egl->window.subsurface);
535
536   if (window->queue_resize) {
537     guint width, height;
538
539     gst_gl_window_get_surface_dimensions (window, &width, &height);
540     gst_gl_window_resize (window, width, height);
541   }
542
543   if (window->draw)
544     window->draw (window->draw_data);
545
546   gst_gl_context_swap_buffers (context);
547
548   if (window_egl->window.subsurface)
549     wl_subsurface_set_desync (window_egl->window.subsurface);
550
551   gst_object_unref (context);
552 }
553
554 static void
555 gst_gl_window_wayland_egl_draw (GstGLWindow * window)
556 {
557   gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, window);
558 }
559
560 struct SetRenderRectangle
561 {
562   GstGLWindowWaylandEGL *window_egl;
563   GstVideoRectangle rect;
564 };
565
566 static void
567 _free_set_render_rectangle (struct SetRenderRectangle *render)
568 {
569   if (render) {
570     if (render->window_egl)
571       gst_object_unref (render->window_egl);
572     g_free (render);
573   }
574 }
575
576 static void
577 _set_render_rectangle (gpointer data)
578 {
579   struct SetRenderRectangle *render = data;
580
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);
583
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;
590   }
591
592   window_resize (render->window_egl, render->rect.w, render->rect.h);
593
594   render->window_egl->window.render_rect = render->rect;
595 }
596
597 static gboolean
598 gst_gl_window_wayland_egl_set_render_rectangle (GstGLWindow * window,
599     gint x, gint y, gint width, gint height)
600 {
601   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
602   struct SetRenderRectangle *render;
603
604   render = g_new0 (struct SetRenderRectangle, 1);
605   render->window_egl = gst_object_ref (window_egl);
606   render->rect.x = x;
607   render->rect.y = y;
608   render->rect.w = width;
609   render->rect.h = height;
610
611   gst_gl_window_send_message_async (window,
612       (GstGLWindowCB) _set_render_rectangle, render,
613       (GDestroyNotify) _free_set_render_rectangle);
614
615   return TRUE;
616 }
617
618 static void
619 gst_gl_window_wayland_egl_set_preferred_size (GstGLWindow * window, gint width,
620     gint height)
621 {
622   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
623
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);
631     }
632   }
633 }
634
635 static guintptr
636 gst_gl_window_wayland_egl_get_display (GstGLWindow * window)
637 {
638   GstGLDisplayWayland *display = GST_GL_DISPLAY_WAYLAND (window->display);
639
640   return (guintptr) display->display;
641 }