1From 094b728bb0ab22c917928d334358b4491b04b163 Mon Sep 17 00:00:00 2001
2From: Jeffy Chen <jeffy.chen@rock-chips.com>
3Date: Tue, 28 Dec 2021 14:06:19 +0800
4Subject: [PATCH 20/41] waylandsink: Support window alpha property
5
6Tested with:
7gst-launch-1.0 videotestsrc ! waylandsink alpha=0.5
8
9Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
10---
11 ext/wayland/gstwaylandsink.c       | 31 ++++++++++++++++++++++++++++++
12 ext/wayland/gstwaylandsink.h       |  1 +
13 gst-libs/gst/wayland/gstwlwindow.c | 10 ++++++++++
14 gst-libs/gst/wayland/gstwlwindow.h |  3 +++
15 4 files changed, 45 insertions(+)
16
17diff --git a/ext/wayland/gstwaylandsink.c b/ext/wayland/gstwaylandsink.c
18index 56a6849..008c774 100644
19--- a/ext/wayland/gstwaylandsink.c
20+++ b/ext/wayland/gstwaylandsink.c
21@@ -62,6 +62,7 @@ enum
22   PROP_FULLSCREEN,
23   PROP_ROTATE_METHOD,
24   PROP_LAYER,
25+  PROP_ALPHA,
26   PROP_LAST
27 };
28
29@@ -202,6 +203,11 @@ gst_wayland_sink_class_init (GstWaylandSinkClass * klass)
30           GST_TYPE_WL_WINDOW_LAYER, GST_WL_WINDOW_LAYER_NORMAL,
31           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
32
33+  g_object_class_install_property (gobject_class, PROP_ALPHA,
34+      g_param_spec_double ("alpha", "Window alpha",
35+          "Wayland window alpha", 0.0, 1.0, 1.0,
36+          G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
37+
38  /**
39   * waylandsink:render-rectangle:
40   *
41@@ -221,6 +227,7 @@ gst_wayland_sink_init (GstWaylandSink * self)
42
43   self->window_handle = 1;
44   self->layer = GST_WL_WINDOW_LAYER_NORMAL;
45+  self->alpha = 1.0;
46 }
47
48 static void
49@@ -284,6 +291,18 @@ gst_wayland_sink_set_layer (GstWaylandSink * self, GstWlWindowLayer layer)
50   g_mutex_unlock (&self->render_lock);
51 }
52
53+static void
54+gst_wayland_sink_set_alpha (GstWaylandSink * self, gdouble alpha)
55+{
56+  if (alpha == self->alpha)
57+    return;
58+
59+  g_mutex_lock (&self->render_lock);
60+  self->alpha = alpha;
61+  gst_wl_window_ensure_alpha (self->window, alpha);
62+  g_mutex_unlock (&self->render_lock);
63+}
64+
65 static void
66 gst_wayland_sink_get_property (GObject * object,
67     guint prop_id, GValue * value, GParamSpec * pspec)
68@@ -311,6 +330,11 @@ gst_wayland_sink_get_property (GObject * object,
69       g_value_set_enum (value, self->layer);
70       GST_OBJECT_UNLOCK (self);
71       break;
72+    case PROP_ALPHA:
73+      GST_OBJECT_LOCK (self);
74+      g_value_set_double (value, self->alpha);
75+      GST_OBJECT_UNLOCK (self);
76+      break;
77     default:
78       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
79       break;
80@@ -343,6 +367,11 @@ gst_wayland_sink_set_property (GObject * object,
81       gst_wayland_sink_set_layer (self, g_value_get_enum (value));
82       GST_OBJECT_UNLOCK (self);
83       break;
84+    case PROP_ALPHA:
85+      GST_OBJECT_LOCK (self);
86+      gst_wayland_sink_set_alpha (self, g_value_get_double (value));
87+      GST_OBJECT_UNLOCK (self);
88+      break;
89     default:
90       if (!gst_video_overlay_set_property (object, PROP_LAST, prop_id, value))
91         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
92@@ -827,6 +856,7 @@ gst_wayland_sink_show_frame (GstVideoSink * vsink, GstBuffer * buffer)
93           G_CALLBACK (on_window_closed), self, 0);
94       gst_wl_window_set_rotate_method (self->window,
95           self->current_rotate_method);
96+      gst_wl_window_ensure_alpha (self->window, self->alpha);
97     }
98   }
99
100@@ -1085,6 +1115,7 @@ gst_wayland_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle)
101             &self->render_lock);
102         gst_wl_window_set_rotate_method (self->window,
103             self->current_rotate_method);
104+        gst_wl_window_ensure_alpha (self->window, self->alpha);
105
106         if (self->last_buffer) {
107           /* Resend video info to force resize video surface */
108diff --git a/ext/wayland/gstwaylandsink.h b/ext/wayland/gstwaylandsink.h
109index a417788..0339ef7 100644
110--- a/ext/wayland/gstwaylandsink.h
111+++ b/ext/wayland/gstwaylandsink.h
112@@ -58,6 +58,7 @@ struct _GstWaylandSink
113   GstVideoInfo video_info;
114   gboolean fullscreen;
115   GstWlWindowLayer layer;
116+  gdouble alpha;
117
118   gchar *display_name;
119
120diff --git a/gst-libs/gst/wayland/gstwlwindow.c b/gst-libs/gst/wayland/gstwlwindow.c
121index fed1c97..f470338 100644
122--- a/gst-libs/gst/wayland/gstwlwindow.c
123+++ b/gst-libs/gst/wayland/gstwlwindow.c
124@@ -24,6 +24,7 @@
125 #include <config.h>
126 #endif
127
128+#include <stdio.h>
129 #include <stdlib.h>
130
131 #include "gstwlwindow.h"
132@@ -267,6 +268,15 @@ gst_wl_window_set_config (GstWlWindow * self, const char *config)
133   xdg_toplevel_set_title (priv->xdg_toplevel, config);
134 }
135
136+void
137+gst_wl_window_ensure_alpha (GstWlWindow * window, gdouble alpha)
138+{
139+  char s[128];
140+
141+  snprintf (s, sizeof (s), "attrs=alpha:%f;", alpha);
142+  gst_wl_window_set_config (window, s);
143+}
144+
145 void
146 gst_wl_window_ensure_layer (GstWlWindow * self, GstWlWindowLayer layer)
147 {
148diff --git a/gst-libs/gst/wayland/gstwlwindow.h b/gst-libs/gst/wayland/gstwlwindow.h
149index a0b05c3..2672e0f 100644
150--- a/gst-libs/gst/wayland/gstwlwindow.h
151+++ b/gst-libs/gst/wayland/gstwlwindow.h
152@@ -40,6 +40,9 @@ struct _GstWlWindow
153   GObject parent_instance;
154 };
155
156+GST_WL_API
157+void gst_wl_window_ensure_alpha (GstWlWindow * self, gdouble alpha);
158+
159 GST_WL_API
160 void gst_wl_window_ensure_layer (GstWlWindow * self,
161         GstWlWindowLayer layer);
162--
1632.20.1
164
165