gl/wayland: correctly use the set_render_rectangle size first
[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.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.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 *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 (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 *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", 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 *shell_surface)
224 {
225 }
226
227 static const struct wl_shell_surface_listener 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.shell_surface) {
241     wl_shell_surface_destroy (window_egl->window.shell_surface);
242     window_egl->window.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
254 static void
255 create_surfaces (GstGLWindowWaylandEGL * window_egl)
256 {
257   GstGLDisplayWayland *display =
258       GST_GL_DISPLAY_WAYLAND (GST_GL_WINDOW (window_egl)->display);
259   gint width, height;
260
261   if (!window_egl->window.surface) {
262     window_egl->window.surface =
263         wl_compositor_create_surface (display->compositor);
264     if (window_egl->window.queue)
265       wl_proxy_set_queue ((struct wl_proxy *) window_egl->window.surface,
266           window_egl->window.queue);
267   }
268
269   if (window_egl->window.foreign_surface) {
270     /* (re)parent */
271     if (!display->subcompositor) {
272       GST_ERROR_OBJECT (window_egl,
273           "Wayland server does not support subsurfaces");
274       window_egl->window.foreign_surface = NULL;
275       goto shell_window;
276     }
277
278     if (!window_egl->window.subsurface) {
279       window_egl->window.subsurface =
280           wl_subcompositor_get_subsurface (display->subcompositor,
281           window_egl->window.surface, window_egl->window.foreign_surface);
282       if (window_egl->window.queue)
283         wl_proxy_set_queue ((struct wl_proxy *) window_egl->window.subsurface,
284             window_egl->window.queue);
285
286       wl_subsurface_set_position (window_egl->window.subsurface,
287           window_egl->window.window_x, window_egl->window.window_y);
288       wl_subsurface_set_desync (window_egl->window.subsurface);
289     }
290   } else {
291   shell_window:
292     if (!window_egl->window.shell_surface) {
293       window_egl->window.shell_surface =
294           wl_shell_get_shell_surface (display->shell,
295           window_egl->window.surface);
296       if (window_egl->window.queue)
297         wl_proxy_set_queue ((struct wl_proxy *) window_egl->
298             window.shell_surface, window_egl->window.queue);
299
300       wl_shell_surface_add_listener (window_egl->window.shell_surface,
301           &shell_surface_listener, window_egl);
302
303       wl_shell_surface_set_title (window_egl->window.shell_surface,
304           "OpenGL Renderer");
305       wl_shell_surface_set_toplevel (window_egl->window.shell_surface);
306     }
307   }
308
309   /*
310    * render_rect is the application requested size so choose that first if
311    * available.
312    * Else choose the already chosen size if set
313    * Else choose the preferred size if set
314    * Else choose a default value
315    */
316   if (window_egl->window.render_rect.w > 0)
317     width = window_egl->window.render_rect.w;
318   else if (window_egl->window.window_width > 0)
319     width = window_egl->window.window_width;
320   else if (window_egl->window.preferred_width > 0)
321     width = window_egl->window.preferred_width;
322   else
323     width = 320;
324   window_egl->window.window_width = width;
325
326   if (window_egl->window.render_rect.h > 0)
327     height = window_egl->window.render_rect.h;
328   else if (window_egl->window.window_height > 0)
329     height = window_egl->window.window_height;
330   else if (window_egl->window.preferred_height > 0)
331     height = window_egl->window.preferred_height;
332   else
333     height = 240;
334   window_egl->window.window_height = height;
335
336   if (!window_egl->window.native) {
337     gst_gl_window_resize (GST_GL_WINDOW (window_egl), width, height);
338
339     window_egl->window.native =
340         wl_egl_window_create (window_egl->window.surface, width, height);
341     if (window_egl->window.queue)
342       wl_proxy_set_queue ((struct wl_proxy *) window_egl->window.native,
343           window_egl->window.queue);
344   }
345 }
346
347 static void
348 gst_gl_window_wayland_egl_class_init (GstGLWindowWaylandEGLClass * klass)
349 {
350   GstGLWindowClass *window_class = (GstGLWindowClass *) klass;
351
352   window_class->get_window_handle =
353       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_get_window_handle);
354   window_class->set_window_handle =
355       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_window_handle);
356   window_class->show = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_show);
357   window_class->draw = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_draw);
358   window_class->close = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_close);
359   window_class->open = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_open);
360   window_class->get_display =
361       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_get_display);
362   window_class->set_render_rectangle =
363       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_render_rectangle);
364   window_class->set_preferred_size =
365       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_preferred_size);
366 }
367
368 static void
369 gst_gl_window_wayland_egl_init (GstGLWindowWaylandEGL * window)
370 {
371   window->window.render_rect.w = window->window.render_rect.h = -1;
372 }
373
374 /* Must be called in the gl thread */
375 GstGLWindowWaylandEGL *
376 gst_gl_window_wayland_egl_new (GstGLDisplay * display)
377 {
378   GstGLWindowWaylandEGL *window;
379
380   if ((gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_WAYLAND)
381       == 0)
382     /* we require a wayland display to create wayland surfaces */
383     return NULL;
384
385   GST_DEBUG ("creating Wayland EGL window");
386
387   window = g_object_new (GST_TYPE_GL_WINDOW_WAYLAND_EGL, NULL);
388   gst_object_ref_sink (window);
389
390   return window;
391 }
392
393 static void
394 gst_gl_window_wayland_egl_close (GstGLWindow * window)
395 {
396   GstGLWindowWaylandEGL *window_egl;
397
398   window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
399
400   destroy_surfaces (window_egl);
401
402   g_source_destroy (window_egl->wl_source);
403   g_source_unref (window_egl->wl_source);
404   window_egl->wl_source = NULL;
405
406   GST_GL_WINDOW_CLASS (parent_class)->close (window);
407 }
408
409 static gboolean
410 gst_gl_window_wayland_egl_open (GstGLWindow * window, GError ** error)
411 {
412   GstGLDisplayWayland *display;
413   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
414
415   if (!GST_IS_GL_DISPLAY_WAYLAND (window->display)) {
416     g_set_error (error, GST_GL_WINDOW_ERROR,
417         GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
418         "Failed to retrieve Wayland display (wrong type?)");
419     return FALSE;
420   }
421   display = GST_GL_DISPLAY_WAYLAND (window->display);
422
423   if (!display->display) {
424     g_set_error (error, GST_GL_WINDOW_ERROR,
425         GST_GL_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
426         "Failed to retrieve Wayland display");
427     return FALSE;
428   }
429
430   window_egl->window.queue = wl_display_create_queue (display->display);
431
432   window_egl->wl_source = wayland_event_source_new (display->display,
433       window_egl->window.queue);
434
435   if (!GST_GL_WINDOW_CLASS (parent_class)->open (window, error))
436     return FALSE;
437
438   g_source_attach (window_egl->wl_source, window->main_context);
439
440   return TRUE;
441 }
442
443 void
444 gst_gl_window_wayland_egl_create_window (GstGLWindowWaylandEGL * window_egl)
445 {
446   create_surfaces (window_egl);
447 }
448
449 static guintptr
450 gst_gl_window_wayland_egl_get_window_handle (GstGLWindow * window)
451 {
452   return (guintptr) GST_GL_WINDOW_WAYLAND_EGL (window)->window.native;
453 }
454
455 static void
456 gst_gl_window_wayland_egl_set_window_handle (GstGLWindow * window,
457     guintptr handle)
458 {
459   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
460   struct wl_surface *surface = (struct wl_surface *) handle;
461
462   /* already set the NULL handle */
463   if (surface == NULL && window_egl->window.foreign_surface == NULL)
464     return;
465
466   /* unparent */
467   destroy_surfaces (window_egl);
468   window_egl->window.foreign_surface = surface;
469   create_surfaces (window_egl);
470 }
471
472 static void
473 _roundtrip_async (GstGLWindow * window)
474 {
475   GstGLDisplayWayland *display_wayland =
476       GST_GL_DISPLAY_WAYLAND (window->display);
477   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
478
479   create_surfaces (window_egl);
480
481   if (gst_gl_wl_display_roundtrip_queue (display_wayland->display,
482           window_egl->window.queue) < 0)
483     GST_WARNING_OBJECT (window, "failed a roundtrip");
484 }
485
486 static void
487 gst_gl_window_wayland_egl_show (GstGLWindow * window)
488 {
489   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
490
491   create_surfaces (window_egl);
492
493   gst_gl_window_send_message (window, (GstGLWindowCB) _roundtrip_async, window);
494 }
495
496 static void
497 window_resize (GstGLWindowWaylandEGL * window_egl, guint width, guint height)
498 {
499   GstGLWindow *window = GST_GL_WINDOW (window_egl);
500
501   GST_DEBUG ("resizing window from %ux%u to %ux%u",
502       window_egl->window.window_width, window_egl->window.window_height, width,
503       height);
504
505   if (window_egl->window.native) {
506     wl_egl_window_resize (window_egl->window.native, width, height, 0, 0);
507   }
508
509   gst_gl_window_resize (window, width, height);
510
511   window_egl->window.window_width = width;
512   window_egl->window.window_height = height;
513 }
514
515 static void
516 draw_cb (gpointer data)
517 {
518   GstGLWindowWaylandEGL *window_egl = data;
519   GstGLWindow *window = GST_GL_WINDOW (window_egl);
520   GstGLContext *context = gst_gl_window_get_context (window);
521
522   create_surfaces (window_egl);
523
524   if (window_egl->window.subsurface)
525     wl_subsurface_set_desync (window_egl->window.subsurface);
526
527   if (window->queue_resize) {
528     guint width, height;
529
530     gst_gl_window_get_surface_dimensions (window, &width, &height);
531     gst_gl_window_resize (window, width, height);
532   }
533
534   if (window->draw)
535     window->draw (window->draw_data);
536
537   gst_gl_context_swap_buffers (context);
538
539   if (window_egl->window.subsurface)
540     wl_subsurface_set_desync (window_egl->window.subsurface);
541
542   gst_object_unref (context);
543 }
544
545 static void
546 gst_gl_window_wayland_egl_draw (GstGLWindow * window)
547 {
548   gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, window);
549 }
550
551 struct SetRenderRectangle
552 {
553   GstGLWindowWaylandEGL *window_egl;
554   GstVideoRectangle rect;
555 };
556
557 static void
558 _free_set_render_rectangle (struct SetRenderRectangle *render)
559 {
560   if (render) {
561     if (render->window_egl)
562       gst_object_unref (render->window_egl);
563     g_free (render);
564   }
565 }
566
567 static void
568 _set_render_rectangle (gpointer data)
569 {
570   struct SetRenderRectangle *render = data;
571
572   GST_LOG_OBJECT (render->window_egl, "setting render rectangle %i,%i+%ix%i",
573       render->rect.x, render->rect.y, render->rect.w, render->rect.h);
574
575   if (render->window_egl->window.subsurface) {
576     wl_subsurface_set_sync (render->window_egl->window.subsurface);
577     wl_subsurface_set_position (render->window_egl->window.subsurface,
578         render->rect.x, render->rect.y);
579     render->window_egl->window.window_x = render->rect.x;
580     render->window_egl->window.window_y = render->rect.y;
581   }
582
583   window_resize (render->window_egl, render->rect.w, render->rect.h);
584
585   render->window_egl->window.render_rect = render->rect;
586 }
587
588 static gboolean
589 gst_gl_window_wayland_egl_set_render_rectangle (GstGLWindow * window,
590     gint x, gint y, gint width, gint height)
591 {
592   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
593   struct SetRenderRectangle *render;
594
595   render = g_new0 (struct SetRenderRectangle, 1);
596   render->window_egl = gst_object_ref (window_egl);
597   render->rect.x = x;
598   render->rect.y = y;
599   render->rect.w = width;
600   render->rect.h = height;
601
602   gst_gl_window_send_message_async (window,
603       (GstGLWindowCB) _set_render_rectangle, render,
604       (GDestroyNotify) _free_set_render_rectangle);
605
606   return TRUE;
607 }
608
609 static void
610 gst_gl_window_wayland_egl_set_preferred_size (GstGLWindow * window, gint width,
611     gint height)
612 {
613   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
614
615   window_egl->window.preferred_width = width;
616   window_egl->window.preferred_height = height;
617   if (window_egl->window.render_rect.w < 0
618       && window_egl->window.render_rect.h < 0) {
619     if (window_egl->window.window_height != height
620         || window_egl->window.window_width != width) {
621       window_resize (window_egl, width, height);
622     }
623   }
624 }
625
626 static guintptr
627 gst_gl_window_wayland_egl_get_display (GstGLWindow * window)
628 {
629   GstGLDisplayWayland *display = GST_GL_DISPLAY_WAYLAND (window->display);
630
631   return (guintptr) display->display;
632 }