From 5c44d0f7282ae768aa6c46ee63ea924847035963 Mon Sep 17 00:00:00 2001
From: Christophe Priouzeau <christophe.priouzeau@foss.st.com>
Date: Tue, 30 May 2023 14:08:58 +0200
Subject: [PATCH 19/19] waylandsink: Emit "map" signal boarder surface is ready

This allows gtkwaylandsink to queue a draw of its gtk widget at the
correct time, avoiding a race.

Signed-off-by: Colin Kinloch <colin.kinloch@collabora.com>
---
 ext/gtk/gstgtkwaylandsink.c        | 13 +++++++++++++
 gst-libs/gst/wayland/gstwlwindow.c |  7 ++++++-
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/ext/gtk/gstgtkwaylandsink.c b/ext/gtk/gstgtkwaylandsink.c
index 985ac07..2debee6 100644
--- a/ext/gtk/gstgtkwaylandsink.c
+++ b/ext/gtk/gstgtkwaylandsink.c
@@ -462,6 +462,17 @@ scrollable_window_adjustment_changed_cb (GtkAdjustment * adjustment,
   return FALSE;
 }
 
+static void
+wl_window_map_cb (GstWlWindow * wl_window, GstGtkWaylandSink * self)
+{
+  GstGtkWaylandSinkPrivate *priv =
+      gst_gtk_wayland_sink_get_instance_private (self);
+
+  GST_DEBUG_OBJECT (self, "waylandsink surface is ready");
+
+  gtk_gst_base_widget_queue_draw (GTK_GST_BASE_WIDGET (priv->gtk_widget));
+}
+
 static void
 setup_wl_window (GstGtkWaylandSink * self)
 {
@@ -488,6 +499,8 @@ setup_wl_window (GstGtkWaylandSink * self)
         wl_surface, &priv->render_lock);
     gst_wl_window_set_rotate_method (priv->wl_window,
         priv->current_rotate_method);
+    g_signal_connect_object (priv->wl_window, "map",
+        G_CALLBACK (wl_window_map_cb), self, 0);
   }
 
   /* In order to position the subsurface correctly within a scrollable widget,
diff --git a/gst-libs/gst/wayland/gstwlwindow.c b/gst-libs/gst/wayland/gstwlwindow.c
index 4aa53c3..afdbc10 100644
--- a/gst-libs/gst/wayland/gstwlwindow.c
+++ b/gst-libs/gst/wayland/gstwlwindow.c
@@ -79,6 +79,7 @@ G_DEFINE_TYPE_WITH_CODE (GstWlWindow, gst_wl_window, G_TYPE_OBJECT,
 enum
 {
   CLOSED,
+  MAP,
   LAST_SIGNAL
 };
 
@@ -155,6 +156,9 @@ gst_wl_window_class_init (GstWlWindowClass * klass)
 
   signals[CLOSED] = g_signal_new ("closed", G_TYPE_FROM_CLASS (gobject_class),
       G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
+  signals[MAP] = g_signal_new ("map", G_TYPE_FROM_CLASS (gobject_class),
+      G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
+
 }
 
 static void
@@ -344,7 +348,7 @@ error:
 
 GstWlWindow *
 gst_wl_window_new_in_surface (GstWlDisplay * display,
-    struct wl_surface * parent, GMutex * render_lock)
+    struct wl_surface *parent, GMutex * render_lock)
 {
   GstWlWindow *self;
   GstWlWindowPrivate *priv;
@@ -509,6 +513,7 @@ gst_wl_window_render (GstWlWindow * self, GstWlBuffer * buffer,
       gst_wl_window_update_borders (self);
       wl_surface_commit (priv->area_surface_wrapper);
       priv->is_area_surface_mapped = TRUE;
+      g_signal_emit (self, signals[MAP], 0);
     }
   } else {
     /* clear both video and parent surfaces */
-- 
2.25.1

