glwindow: pass display to implementation's _new()
[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 const gchar *WlEGLErrorString ();
39
40 #define GST_CAT_DEFAULT gst_gl_window_debug
41
42 #define gst_gl_window_wayland_egl_parent_class parent_class
43 G_DEFINE_TYPE (GstGLWindowWaylandEGL, gst_gl_window_wayland_egl,
44     GST_GL_TYPE_WINDOW);
45
46 static guintptr gst_gl_window_wayland_egl_get_window_handle (GstGLWindow *
47     window);
48 static void gst_gl_window_wayland_egl_set_window_handle (GstGLWindow * window,
49     guintptr handle);
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,
54     GError ** error);
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);
58
59 #if 0
60 static void
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)
63 {
64   GstGLWindowWaylandEGL *window_egl = data;
65   struct wl_buffer *buffer;
66   struct wl_cursor_image *image = NULL;
67
68   window_egl->display.serial = serial;
69
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);
79   }
80 }
81
82 static void
83 pointer_handle_leave (void *data, struct wl_pointer *pointer, uint32_t serial,
84     struct wl_surface *surface)
85 {
86   GstGLWindowWaylandEGL *window_egl = data;
87   window_egl->display.serial = serial;
88 }
89
90 static void
91 pointer_handle_motion (void *data, struct wl_pointer *pointer, uint32_t time,
92     wl_fixed_t sx_w, wl_fixed_t sy_w)
93 {
94   GstGLWindowWaylandEGL *window_egl = data;
95
96   window_egl->display.pointer_x = wl_fixed_to_double (sx_w);
97   window_egl->display.pointer_y = wl_fixed_to_double (sy_w);
98 }
99
100 enum wl_edges
101 {
102   WL_EDGE_NONE = 0,
103   WL_EDGE_TOP = 1,
104   WL_EDGE_BOTTOM = 2,
105   WL_EDGE_LEFT = 4,
106   WL_EDGE_RIGHT = 8,
107 };
108
109 static guint
110 _get_closest_pointer_corner (GstGLWindowWaylandEGL * window_egl)
111 {
112   guint edges = 0;
113   gdouble win_width, win_height;
114   gdouble p_x, p_y;
115
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;
120
121   if (win_width == 0.0 || win_height == 0.0)
122     return WL_EDGE_NONE;
123
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;
126
127   return edges;
128 }
129
130 static void
131 pointer_handle_button (void *data, struct wl_pointer *pointer, uint32_t serial,
132     uint32_t time, uint32_t button, uint32_t state_w)
133 {
134   GstGLWindowWaylandEGL *window_egl = data;
135   guint edges = _get_closest_pointer_corner (window_egl);
136   window_egl->display.serial = serial;
137
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);
141
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);
145 }
146
147 static void
148 pointer_handle_axis (void *data, struct wl_pointer *pointer, uint32_t time,
149     uint32_t axis, wl_fixed_t value)
150 {
151 }
152
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,
158   pointer_handle_axis,
159 };
160
161 static void
162 seat_handle_capabilities (void *data, struct wl_seat *seat,
163     enum wl_seat_capability caps)
164 {
165   GstGLWindowWaylandEGL *window_egl = data;
166   struct display *display = &window_egl->display;
167
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;
175   }
176 #if 0
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;
184   }
185 #endif
186 }
187
188 static const struct wl_seat_listener seat_listener = {
189   seat_handle_capabilities,
190 };
191 #endif
192 static void
193 handle_ping (void *data, struct wl_shell_surface *shell_surface,
194     uint32_t serial)
195 {
196   GstGLWindowWaylandEGL *window_egl = data;
197
198   GST_TRACE_OBJECT (window_egl, "ping received serial %u", serial);
199
200   wl_shell_surface_pong (shell_surface, serial);
201 }
202
203 static void window_resize (GstGLWindowWaylandEGL * window_egl, guint width,
204     guint height);
205
206 static void
207 handle_configure (void *data, struct wl_shell_surface *shell_surface,
208     uint32_t edges, int32_t width, int32_t height)
209 {
210   GstGLWindowWaylandEGL *window_egl = data;
211
212   GST_DEBUG ("configure event on surface %p, %ix%i", shell_surface, width,
213       height);
214
215   window_resize (window_egl, width, height);
216 }
217
218 static void
219 handle_popup_done (void *data, struct wl_shell_surface *shell_surface)
220 {
221 }
222
223 static const struct wl_shell_surface_listener shell_surface_listener = {
224   handle_ping,
225   handle_configure,
226   handle_popup_done
227 };
228
229 static void
230 destroy_surfaces (GstGLWindowWaylandEGL * window_egl)
231 {
232   if (window_egl->window.subsurface) {
233     wl_subsurface_destroy (window_egl->window.subsurface);
234     window_egl->window.subsurface = NULL;
235   }
236   if (window_egl->window.shell_surface) {
237     wl_shell_surface_destroy (window_egl->window.shell_surface);
238     window_egl->window.shell_surface = NULL;
239   }
240   if (window_egl->window.surface) {
241     wl_surface_destroy (window_egl->window.surface);
242     window_egl->window.surface = NULL;
243   }
244   if (window_egl->window.native) {
245     wl_egl_window_destroy (window_egl->window.native);
246     window_egl->window.native = NULL;
247   }
248 }
249
250 static void
251 create_surfaces (GstGLWindowWaylandEGL * window_egl)
252 {
253   GstGLDisplayWayland *display =
254       GST_GL_DISPLAY_WAYLAND (GST_GL_WINDOW (window_egl)->display);
255   gint width, height;
256
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);
263   }
264
265   if (window_egl->window.foreign_surface) {
266     /* (re)parent */
267     if (!display->subcompositor) {
268       GST_ERROR_OBJECT (window_egl,
269           "Wayland server does not support subsurfaces");
270       window_egl->window.foreign_surface = NULL;
271       goto shell_window;
272     }
273
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);
281
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);
285     }
286   } else {
287   shell_window:
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);
295
296       wl_shell_surface_add_listener (window_egl->window.shell_surface,
297           &shell_surface_listener, window_egl);
298
299       wl_shell_surface_set_title (window_egl->window.shell_surface,
300           "OpenGL Renderer");
301       wl_shell_surface_set_toplevel (window_egl->window.shell_surface);
302     }
303   }
304
305   if (window_egl->window.window_width > 0)
306     width = window_egl->window.window_width;
307   else
308     width = 320;
309   window_egl->window.window_width = width;
310
311   if (window_egl->window.window_height > 0)
312     height = window_egl->window.window_height;
313   else
314     height = 240;
315   window_egl->window.window_height = height;
316
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);
323   }
324 }
325
326 static void
327 gst_gl_window_wayland_egl_class_init (GstGLWindowWaylandEGLClass * klass)
328 {
329   GstGLWindowClass *window_class = (GstGLWindowClass *) klass;
330
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);
345 }
346
347 static void
348 gst_gl_window_wayland_egl_init (GstGLWindowWaylandEGL * window)
349 {
350 }
351
352 /* Must be called in the gl thread */
353 GstGLWindowWaylandEGL *
354 gst_gl_window_wayland_egl_new (GstGLDisplay * display)
355 {
356   if ((gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_WAYLAND)
357       == 0)
358     /* we require a wayland display to create wayland surfaces */
359     return NULL;
360
361   GST_DEBUG ("creating Wayland EGL window");
362
363   return g_object_new (GST_GL_TYPE_WINDOW_WAYLAND_EGL, NULL);
364 }
365
366 static void
367 gst_gl_window_wayland_egl_close (GstGLWindow * window)
368 {
369   GstGLWindowWaylandEGL *window_egl;
370
371   window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
372
373   destroy_surfaces (window_egl);
374
375   g_source_destroy (window_egl->wl_source);
376   g_source_unref (window_egl->wl_source);
377   window_egl->wl_source = NULL;
378
379   GST_GL_WINDOW_CLASS (parent_class)->close (window);
380 }
381
382 static gboolean
383 gst_gl_window_wayland_egl_open (GstGLWindow * window, GError ** error)
384 {
385   GstGLDisplayWayland *display = GST_GL_DISPLAY_WAYLAND (window->display);
386   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
387
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");
392     return FALSE;
393   }
394
395   window_egl->window.queue = wl_display_create_queue (display->display);
396
397   window_egl->wl_source = wayland_event_source_new (display->display,
398       window_egl->window.queue);
399
400   if (!GST_GL_WINDOW_CLASS (parent_class)->open (window, error))
401     return FALSE;
402
403   g_source_attach (window_egl->wl_source, g_main_context_get_thread_default ());
404
405   return TRUE;
406 }
407
408 void
409 gst_gl_window_wayland_egl_create_window (GstGLWindowWaylandEGL * window_egl)
410 {
411   create_surfaces (window_egl);
412 }
413
414 static guintptr
415 gst_gl_window_wayland_egl_get_window_handle (GstGLWindow * window)
416 {
417   return (guintptr) GST_GL_WINDOW_WAYLAND_EGL (window)->window.native;
418 }
419
420 static void
421 gst_gl_window_wayland_egl_set_window_handle (GstGLWindow * window,
422     guintptr handle)
423 {
424   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
425   struct wl_surface *surface = (struct wl_surface *) handle;
426
427   /* already set the NULL handle */
428   if (surface == NULL && window_egl->window.foreign_surface == NULL)
429     return;
430
431   /* unparent */
432   destroy_surfaces (window_egl);
433   window_egl->window.foreign_surface = surface;
434   create_surfaces (window_egl);
435 }
436
437 static void
438 gst_gl_window_wayland_egl_show (GstGLWindow * window)
439 {
440   GstGLDisplayWayland *display_wayland =
441       GST_GL_DISPLAY_WAYLAND (window->display);
442   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
443
444   create_surfaces (window_egl);
445
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");
449 }
450
451 static void
452 window_resize (GstGLWindowWaylandEGL * window_egl, guint width, guint height)
453 {
454   GstGLWindow *window = GST_GL_WINDOW (window_egl);
455
456   GST_DEBUG ("resizing window from %ux%u to %ux%u",
457       window_egl->window.window_width, window_egl->window.window_height, width,
458       height);
459
460   if (window_egl->window.native) {
461     wl_egl_window_resize (window_egl->window.native, width, height, 0, 0);
462   }
463
464   if (window->resize)
465     window->resize (window->resize_data, width, height);
466
467   window_egl->window.window_width = width;
468   window_egl->window.window_height = height;
469 }
470
471 static void
472 draw_cb (gpointer data)
473 {
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);
478
479   create_surfaces (window_egl);
480
481   if (window_egl->window.subsurface)
482     wl_subsurface_set_desync (window_egl->window.subsurface);
483
484   if (window->draw)
485     window->draw (window->draw_data);
486
487   context_class->swap_buffers (context);
488
489   if (window_egl->window.subsurface)
490     wl_subsurface_set_desync (window_egl->window.subsurface);
491
492   gst_object_unref (context);
493 }
494
495 static void
496 gst_gl_window_wayland_egl_draw (GstGLWindow * window)
497 {
498   gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, window);
499 }
500
501 struct SetRenderRectangle
502 {
503   GstGLWindowWaylandEGL *window_egl;
504   GstVideoRectangle rect;
505 };
506
507 static void
508 _free_set_render_rectangle (struct SetRenderRectangle *render)
509 {
510   if (render) {
511     if (render->window_egl)
512       gst_object_unref (render->window_egl);
513     g_free (render);
514   }
515 }
516
517 static void
518 _set_render_rectangle (gpointer data)
519 {
520   struct SetRenderRectangle *render = data;
521
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);
524
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;
531   }
532
533   window_resize (render->window_egl, render->rect.w, render->rect.h);
534 }
535
536 static gboolean
537 gst_gl_window_wayland_egl_set_render_rectangle (GstGLWindow * window,
538     gint x, gint y, gint width, gint height)
539 {
540   GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
541   struct SetRenderRectangle *render;
542
543   render = g_new0 (struct SetRenderRectangle, 1);
544   render->window_egl = gst_object_ref (window_egl);
545   render->rect.x = x;
546   render->rect.y = y;
547   render->rect.w = width;
548   render->rect.h = height;
549
550   gst_gl_window_send_message_async (window,
551       (GstGLWindowCB) _set_render_rectangle, render,
552       (GDestroyNotify) _free_set_render_rectangle);
553
554   return TRUE;
555 }
556
557 static guintptr
558 gst_gl_window_wayland_egl_get_display (GstGLWindow * window)
559 {
560   GstGLDisplayWayland *display = GST_GL_DISPLAY_WAYLAND (window->display);
561
562   return (guintptr) display->display;
563 }