gl/wayland: Don't prefix wl_shell struct field
[platform/upstream/gstreamer.git] / gst-libs / gst / gl / wayland / gstglwindow_wayland_egl.c
index 592c03c..ee7a91f 100644 (file)
 #include <gst/gl/gstglcontext.h>
 
 #include "gstgldisplay_wayland.h"
+#include "gstgldisplay_wayland_private.h"
 #include "gstglwindow_wayland_egl.h"
 
+#include "../gstglwindow_private.h"
+
 const gchar *WlEGLErrorString ();
 
 #define GST_CAT_DEFAULT gst_gl_window_debug
 
 #define gst_gl_window_wayland_egl_parent_class parent_class
 G_DEFINE_TYPE (GstGLWindowWaylandEGL, gst_gl_window_wayland_egl,
-    GST_GL_TYPE_WINDOW);
+    GST_TYPE_GL_WINDOW);
 
 static guintptr gst_gl_window_wayland_egl_get_window_handle (GstGLWindow *
     window);
@@ -55,6 +58,8 @@ static gboolean gst_gl_window_wayland_egl_open (GstGLWindow * window,
 static guintptr gst_gl_window_wayland_egl_get_display (GstGLWindow * window);
 static gboolean gst_gl_window_wayland_egl_set_render_rectangle (GstGLWindow *
     window, gint x, gint y, gint width, gint height);
+static void gst_gl_window_wayland_egl_set_preferred_size (GstGLWindow * window,
+    gint width, gint height);
 
 #if 0
 static void
@@ -136,11 +141,11 @@ pointer_handle_button (void *data, struct wl_pointer *pointer, uint32_t serial,
   window_egl->display.serial = serial;
 
   if (button == BTN_LEFT && state_w == WL_POINTER_BUTTON_STATE_PRESSED)
-    wl_shell_surface_move (window_egl->window.shell_surface,
+    wl_shell_surface_move (window_egl->window.wl_shell_surface,
         window_egl->display.seat, serial);
 
   if (button == BTN_RIGHT && state_w == WL_POINTER_BUTTON_STATE_PRESSED)
-    wl_shell_surface_resize (window_egl->window.shell_surface,
+    wl_shell_surface_resize (window_egl->window.wl_shell_surface,
         window_egl->display.seat, serial, edges);
 }
 
@@ -190,61 +195,167 @@ static const struct wl_seat_listener seat_listener = {
 };
 #endif
 static void
-handle_ping (void *data, struct wl_shell_surface *shell_surface,
+handle_ping (void *data, struct wl_shell_surface *wl_shell_surface,
     uint32_t serial)
 {
   GstGLWindowWaylandEGL *window_egl = data;
 
   GST_TRACE_OBJECT (window_egl, "ping received serial %u", serial);
 
-  wl_shell_surface_pong (shell_surface, serial);
+  wl_shell_surface_pong (wl_shell_surface, serial);
 }
 
 static void window_resize (GstGLWindowWaylandEGL * window_egl, guint width,
     guint height);
 
 static void
-handle_configure (void *data, struct wl_shell_surface *shell_surface,
+handle_configure (void *data, struct wl_shell_surface *wl_shell_surface,
     uint32_t edges, int32_t width, int32_t height)
 {
   GstGLWindowWaylandEGL *window_egl = data;
 
-  GST_DEBUG ("configure event on surface %p, %ix%i", shell_surface, width,
+  GST_DEBUG ("configure event on surface %p, %ix%i", wl_shell_surface, width,
       height);
 
   window_resize (window_egl, width, height);
 }
 
 static void
-handle_popup_done (void *data, struct wl_shell_surface *shell_surface)
+handle_popup_done (void *data, struct wl_shell_surface *wl_shell_surface)
 {
 }
 
-static const struct wl_shell_surface_listener shell_surface_listener = {
+static const struct wl_shell_surface_listener wl_shell_surface_listener = {
   handle_ping,
   handle_configure,
   handle_popup_done
 };
 
 static void
-destroy_surfaces (GstGLWindowWaylandEGL * window_egl)
+handle_xdg_toplevel_close (void *data, struct xdg_toplevel *xdg_toplevel)
+{
+  GstGLWindowWaylandEGL *window_egl = data;
+
+  GST_DEBUG ("XDG toplevel got a \"close\" event.");
+
+  gst_gl_window_wayland_egl_close (GST_GL_WINDOW (window_egl));
+}
+
+static void
+handle_xdg_toplevel_configure (void *data, struct xdg_toplevel *xdg_toplevel,
+    int32_t width, int32_t height, struct wl_array *states)
 {
-  if (window_egl->window.subsurface) {
-    wl_subsurface_destroy (window_egl->window.subsurface);
-    window_egl->window.subsurface = NULL;
+  GstGLWindowWaylandEGL *window_egl = data;
+  const uint32_t *state;
+
+  GST_DEBUG ("configure event on XDG toplevel %p, %ix%i", xdg_toplevel,
+      width, height);
+
+  wl_array_for_each (state, states) {
+    switch (*state) {
+      case XDG_TOPLEVEL_STATE_FULLSCREEN:
+        window_egl->window.fullscreen = TRUE;
+        break;
+      case XDG_TOPLEVEL_STATE_MAXIMIZED:
+      case XDG_TOPLEVEL_STATE_RESIZING:
+      case XDG_TOPLEVEL_STATE_ACTIVATED:
+        break;
+    }
   }
-  if (window_egl->window.shell_surface) {
-    wl_shell_surface_destroy (window_egl->window.shell_surface);
-    window_egl->window.shell_surface = NULL;
+
+  if (width > 0 && height > 0)
+    window_resize (window_egl, width, height);
+}
+
+static const struct xdg_toplevel_listener xdg_toplevel_listener = {
+  handle_xdg_toplevel_configure,
+  handle_xdg_toplevel_close,
+};
+
+static void
+handle_xdg_surface_configure (void *data, struct xdg_surface *xdg_surface,
+    uint32_t serial)
+{
+  xdg_surface_ack_configure (xdg_surface, serial);
+}
+
+static const struct xdg_surface_listener xdg_surface_listener = {
+  handle_xdg_surface_configure,
+};
+
+static void
+destroy_surfaces (GstGLWindowWaylandEGL * window_egl)
+{
+  g_clear_pointer (&window_egl->window.subsurface, wl_subsurface_destroy);
+  g_clear_pointer (&window_egl->window.xdg_toplevel, xdg_toplevel_destroy);
+  g_clear_pointer (&window_egl->window.xdg_surface, xdg_surface_destroy);
+  g_clear_pointer (&window_egl->window.wl_shell_surface,
+      wl_shell_surface_destroy);
+  g_clear_pointer (&window_egl->window.surface, wl_surface_destroy);
+  g_clear_pointer (&window_egl->window.native, wl_egl_window_destroy);
+}
+
+static void
+create_xdg_surface_and_toplevel (GstGLWindowWaylandEGL * window_egl)
+{
+  GstGLDisplayWayland *display =
+      GST_GL_DISPLAY_WAYLAND (GST_GL_WINDOW (window_egl)->display);
+  struct xdg_wm_base *xdg_wm_base;
+  struct xdg_surface *xdg_surface;
+  struct xdg_toplevel *xdg_toplevel;
+
+  GST_DEBUG ("Creating surfaces XDG-shell");
+
+  /* First create the XDG surface */
+  xdg_wm_base = gst_gl_display_wayland_get_xdg_wm_base (display);
+  xdg_surface = xdg_wm_base_get_xdg_surface (xdg_wm_base,
+      window_egl->window.surface);
+  if (window_egl->window.queue) {
+    wl_proxy_set_queue ((struct wl_proxy *) xdg_surface,
+        window_egl->window.queue);
   }
-  if (window_egl->window.surface) {
-    wl_surface_destroy (window_egl->window.surface);
-    window_egl->window.surface = NULL;
+  xdg_surface_add_listener (xdg_surface, &xdg_surface_listener, window_egl);
+
+  /* Then the XDG top-level */
+  xdg_toplevel = xdg_surface_get_toplevel (xdg_surface);
+  xdg_toplevel_set_title (xdg_toplevel, "OpenGL Renderer");
+  if (window_egl->window.queue) {
+    wl_proxy_set_queue ((struct wl_proxy *) xdg_toplevel,
+        window_egl->window.queue);
   }
-  if (window_egl->window.native) {
-    wl_egl_window_destroy (window_egl->window.native);
-    window_egl->window.native = NULL;
+  xdg_toplevel_add_listener (xdg_toplevel, &xdg_toplevel_listener, window_egl);
+
+  /* Commit the xdg_surface state */
+  wl_surface_commit (window_egl->window.surface);
+
+  /* And save them into the fields */
+  window_egl->window.xdg_surface = xdg_surface;
+  window_egl->window.xdg_toplevel = xdg_toplevel;
+}
+
+static void
+create_wl_shell_surface (GstGLWindowWaylandEGL * window_egl)
+{
+  GstGLDisplayWayland *display =
+      GST_GL_DISPLAY_WAYLAND (GST_GL_WINDOW (window_egl)->display);
+  struct wl_shell_surface *wl_shell_surface;
+
+  GST_DEBUG ("Creating surfaces for wl-shell");
+
+  wl_shell_surface = wl_shell_get_shell_surface (display->shell,
+      window_egl->window.surface);
+
+  if (window_egl->window.queue) {
+    wl_proxy_set_queue ((struct wl_proxy *) wl_shell_surface,
+        window_egl->window.queue);
   }
+
+  wl_shell_surface_add_listener (wl_shell_surface, &wl_shell_surface_listener,
+      window_egl);
+  wl_shell_surface_set_title (wl_shell_surface, "OpenGL Renderer");
+  wl_shell_surface_set_toplevel (wl_shell_surface);
+
+  window_egl->window.wl_shell_surface = wl_shell_surface;
 }
 
 static void
@@ -285,36 +396,44 @@ create_surfaces (GstGLWindowWaylandEGL * window_egl)
     }
   } else {
   shell_window:
-    if (!window_egl->window.shell_surface) {
-      window_egl->window.shell_surface =
-          wl_shell_get_shell_surface (display->shell,
-          window_egl->window.surface);
-      if (window_egl->window.queue)
-        wl_proxy_set_queue ((struct wl_proxy *) window_egl->window.
-            shell_surface, window_egl->window.queue);
-
-      wl_shell_surface_add_listener (window_egl->window.shell_surface,
-          &shell_surface_listener, window_egl);
-
-      wl_shell_surface_set_title (window_egl->window.shell_surface,
-          "OpenGL Renderer");
-      wl_shell_surface_set_toplevel (window_egl->window.shell_surface);
+    if (gst_gl_display_wayland_get_xdg_wm_base (display)) {
+      if (!window_egl->window.xdg_surface)
+        create_xdg_surface_and_toplevel (window_egl);
+    } else if (!window_egl->window.wl_shell_surface) {
+      create_wl_shell_surface (window_egl);
     }
   }
 
-  if (window_egl->window.window_width > 0)
+  /*
+   * render_rect is the application requested size so choose that first if
+   * available.
+   * Else choose the already chosen size if set
+   * Else choose the preferred size if set
+   * Else choose a default value
+   */
+  if (window_egl->window.render_rect.w > 0)
+    width = window_egl->window.render_rect.w;
+  else if (window_egl->window.window_width > 0)
     width = window_egl->window.window_width;
+  else if (window_egl->window.preferred_width > 0)
+    width = window_egl->window.preferred_width;
   else
     width = 320;
   window_egl->window.window_width = width;
 
-  if (window_egl->window.window_height > 0)
+  if (window_egl->window.render_rect.h > 0)
+    height = window_egl->window.render_rect.h;
+  else if (window_egl->window.window_height > 0)
     height = window_egl->window.window_height;
+  else if (window_egl->window.preferred_height > 0)
+    height = window_egl->window.preferred_height;
   else
     height = 240;
   window_egl->window.window_height = height;
 
   if (!window_egl->window.native) {
+    gst_gl_window_resize (GST_GL_WINDOW (window_egl), width, height);
+
     window_egl->window.native =
         wl_egl_window_create (window_egl->window.surface, width, height);
     if (window_egl->window.queue)
@@ -332,8 +451,6 @@ gst_gl_window_wayland_egl_class_init (GstGLWindowWaylandEGLClass * klass)
       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_get_window_handle);
   window_class->set_window_handle =
       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_window_handle);
-  window_class->draw_unlocked =
-      GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_draw);
   window_class->show = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_show);
   window_class->draw = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_draw);
   window_class->close = GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_close);
@@ -342,17 +459,22 @@ gst_gl_window_wayland_egl_class_init (GstGLWindowWaylandEGLClass * klass)
       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_get_display);
   window_class->set_render_rectangle =
       GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_render_rectangle);
+  window_class->set_preferred_size =
+      GST_DEBUG_FUNCPTR (gst_gl_window_wayland_egl_set_preferred_size);
 }
 
 static void
 gst_gl_window_wayland_egl_init (GstGLWindowWaylandEGL * window)
 {
+  window->window.render_rect.w = window->window.render_rect.h = -1;
 }
 
 /* Must be called in the gl thread */
 GstGLWindowWaylandEGL *
 gst_gl_window_wayland_egl_new (GstGLDisplay * display)
 {
+  GstGLWindowWaylandEGL *window;
+
   if ((gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_WAYLAND)
       == 0)
     /* we require a wayland display to create wayland surfaces */
@@ -360,7 +482,10 @@ gst_gl_window_wayland_egl_new (GstGLDisplay * display)
 
   GST_DEBUG ("creating Wayland EGL window");
 
-  return g_object_new (GST_GL_TYPE_WINDOW_WAYLAND_EGL, NULL);
+  window = g_object_new (GST_TYPE_GL_WINDOW_WAYLAND_EGL, NULL);
+  gst_object_ref_sink (window);
+
+  return window;
 }
 
 static void
@@ -408,7 +533,7 @@ gst_gl_window_wayland_egl_open (GstGLWindow * window, GError ** error)
   if (!GST_GL_WINDOW_CLASS (parent_class)->open (window, error))
     return FALSE;
 
-  g_source_attach (window_egl->wl_source, g_main_context_get_thread_default ());
+  g_source_attach (window_egl->wl_source, window->main_context);
 
   return TRUE;
 }
@@ -443,7 +568,7 @@ gst_gl_window_wayland_egl_set_window_handle (GstGLWindow * window,
 }
 
 static void
-gst_gl_window_wayland_egl_show (GstGLWindow * window)
+_roundtrip_async (GstGLWindow * window)
 {
   GstGLDisplayWayland *display_wayland =
       GST_GL_DISPLAY_WAYLAND (window->display);
@@ -457,6 +582,16 @@ gst_gl_window_wayland_egl_show (GstGLWindow * window)
 }
 
 static void
+gst_gl_window_wayland_egl_show (GstGLWindow * window)
+{
+  GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
+
+  create_surfaces (window_egl);
+
+  gst_gl_window_send_message (window, (GstGLWindowCB) _roundtrip_async, window);
+}
+
+static void
 window_resize (GstGLWindowWaylandEGL * window_egl, guint width, guint height)
 {
   GstGLWindow *window = GST_GL_WINDOW (window_egl);
@@ -469,8 +604,7 @@ window_resize (GstGLWindowWaylandEGL * window_egl, guint width, guint height)
     wl_egl_window_resize (window_egl->window.native, width, height, 0, 0);
   }
 
-  if (window->resize)
-    window->resize (window->resize_data, width, height);
+  gst_gl_window_resize (window, width, height);
 
   window_egl->window.window_width = width;
   window_egl->window.window_height = height;
@@ -482,17 +616,23 @@ draw_cb (gpointer data)
   GstGLWindowWaylandEGL *window_egl = data;
   GstGLWindow *window = GST_GL_WINDOW (window_egl);
   GstGLContext *context = gst_gl_window_get_context (window);
-  GstGLContextClass *context_class = GST_GL_CONTEXT_GET_CLASS (context);
 
   create_surfaces (window_egl);
 
   if (window_egl->window.subsurface)
     wl_subsurface_set_desync (window_egl->window.subsurface);
 
+  if (window->queue_resize) {
+    guint width, height;
+
+    gst_gl_window_get_surface_dimensions (window, &width, &height);
+    gst_gl_window_resize (window, width, height);
+  }
+
   if (window->draw)
     window->draw (window->draw_data);
 
-  context_class->swap_buffers (context);
+  gst_gl_context_swap_buffers (context);
 
   if (window_egl->window.subsurface)
     wl_subsurface_set_desync (window_egl->window.subsurface);
@@ -539,6 +679,8 @@ _set_render_rectangle (gpointer data)
   }
 
   window_resize (render->window_egl, render->rect.w, render->rect.h);
+
+  render->window_egl->window.render_rect = render->rect;
 }
 
 static gboolean
@@ -562,6 +704,23 @@ gst_gl_window_wayland_egl_set_render_rectangle (GstGLWindow * window,
   return TRUE;
 }
 
+static void
+gst_gl_window_wayland_egl_set_preferred_size (GstGLWindow * window, gint width,
+    gint height)
+{
+  GstGLWindowWaylandEGL *window_egl = GST_GL_WINDOW_WAYLAND_EGL (window);
+
+  window_egl->window.preferred_width = width;
+  window_egl->window.preferred_height = height;
+  if (window_egl->window.render_rect.w < 0
+      && window_egl->window.render_rect.h < 0) {
+    if (window_egl->window.window_height != height
+        || window_egl->window.window_width != width) {
+      window_resize (window_egl, width, height);
+    }
+  }
+}
+
 static guintptr
 gst_gl_window_wayland_egl_get_display (GstGLWindow * window)
 {