Debian Codegraph @

gst-libs/gst/vulkan/wayland/gstvkdisplay_wayland.c

raw symbols SCIP
1 /*
2 * GStreamer
3 * Copyright (C) 2016 Matthew Waters <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/vulkan/wayland/gstvkdisplay_wayland.h>
26 #include "gstvkdisplay_wayland_private.h"
27
28 #include "wayland_event_source.h"
29
30 GST_DEBUG_CATEGORY_STATIC (gst_vulkan_display_wayland_debug);
31 #define GST_CAT_DEFAULT gst_vulkan_display_wayland_debug
32
33 G_DEFINE_TYPE_WITH_CODE (GstVulkanDisplayWayland, gst_vulkan_display_wayland,
34 GST_TYPE_VULKAN_DISPLAY, G_ADD_PRIVATE (GstVulkanDisplayWayland);
35 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT,
36 "vulkandisplaywayland", 0, "Vulkan Wayland Display");
37 );
38
39 static void gst_vulkan_display_wayland_finalize (GObject * object);
40 static gpointer gst_vulkan_display_wayland_get_handle (GstVulkanDisplay *
41 display);
42
43 static void
44 handle_xdg_wm_base_ping (void *user_data, struct xdg_wm_base *xdg_wm_base,
45 uint32_t serial)
46 {
47 xdg_wm_base_pong (xdg_wm_base, serial);
48 }
49
50 static const struct xdg_wm_base_listener xdg_wm_base_listener = {
51 handle_xdg_wm_base_ping
52 };
53
54 static void
55 registry_handle_global (void *data, struct wl_registry *registry,
56 uint32_t name, const char *interface, uint32_t version)
57 {
58 GstVulkanDisplayWayland *display = data;
59 GstVulkanDisplayWaylandPrivate *priv =
60 gst_vulkan_display_wayland_get_instance_private (display);
61
62 GST_TRACE_OBJECT (display, "registry_handle_global with registry %p, "
63 "interface %s, version %u", registry, interface, version);
64
65 if (g_strcmp0 (interface, "wl_compositor") == 0) {
66 display->compositor =
67 wl_registry_bind (registry, name, &wl_compositor_interface, 1);
68 } else if (g_strcmp0 (interface, "wl_subcompositor") == 0) {
69 display->subcompositor =
70 wl_registry_bind (registry, name, &wl_subcompositor_interface, 1);
71 } else if (g_strcmp0 (interface, "wl_shell") == 0) {
72 display->shell = wl_registry_bind (registry, name, &wl_shell_interface, 1);
73 } else if (g_strcmp0 (interface, "xdg_wm_base") == 0) {
74 priv->xdg_wm_base =
75 wl_registry_bind (registry, name, &xdg_wm_base_interface, 1);
76 xdg_wm_base_add_listener (priv->xdg_wm_base, &xdg_wm_base_listener,
77 display);
78 }
79 }
80
81 static void
82 registry_handle_global_remove (void *data, struct wl_registry *registry,
83 uint32_t name)
84 {
85 GstVulkanDisplayWayland *window_wayland = data;
86
87 /* TODO: deal with any registry objects that may be removed */
88 GST_TRACE_OBJECT (window_wayland, "wl_registry %p global_remove %"
89 G_GUINT32_FORMAT, registry, name);
90 }
91
92 static const struct wl_registry_listener registry_listener = {
93 registry_handle_global,
94 registry_handle_global_remove,
95 };
96
97 static void
98 _connect_listeners (GstVulkanDisplayWayland * display)
99 {
100 display->registry = wl_display_get_registry (display->display);
101 wl_registry_add_listener (display->registry, &registry_listener, display);
102
103 wl_display_roundtrip (display->display);
104 }
105
106 GstVulkanDisplayWaylandPrivate *
107 gst_vulkan_display_wayland_get_private (GstVulkanDisplayWayland *
108 display_wayland)
109 {
110 return gst_vulkan_display_wayland_get_instance_private (display_wayland);
111 }
112
113 static void
114 gst_vulkan_display_wayland_class_init (GstVulkanDisplayWaylandClass * klass)
115 {
116 GST_VULKAN_DISPLAY_CLASS (klass)->get_handle =
117 GST_DEBUG_FUNCPTR (gst_vulkan_display_wayland_get_handle);
118
119 G_OBJECT_CLASS (klass)->finalize = gst_vulkan_display_wayland_finalize;
120 }
121
122 static void
123 gst_vulkan_display_wayland_init (GstVulkanDisplayWayland * display_wayland)
124 {
125 GstVulkanDisplay *display = (GstVulkanDisplay *) display_wayland;
126
127 display->type = GST_VULKAN_DISPLAY_TYPE_WAYLAND;
128 display_wayland->foreign_display = FALSE;
129 }
130
131 static void
132 gst_vulkan_display_wayland_finalize (GObject * object)
133 {
134 GstVulkanDisplayWayland *display_wayland =
135 GST_VULKAN_DISPLAY_WAYLAND (object);
136
137 if (!display_wayland->foreign_display && display_wayland->display) {
138 wl_display_flush (display_wayland->display);
139 wl_display_disconnect (display_wayland->display);
140 }
141
142 G_OBJECT_CLASS (gst_vulkan_display_wayland_parent_class)->finalize (object);
143 }
144
145 /**
146 * gst_vulkan_display_wayland_new:
147 * @name: (nullable): a display name
148 *
149 * Create a new #GstVulkanDisplayWayland from the wayland display name. See `wl_display_connect`()
150 * for details on what is a valid name.
151 *
152 * Returns: (transfer full) (nullable): a new #GstVulkanDisplayWayland or %NULL
153 *
154 * Since: 1.18
155 */
156 GstVulkanDisplayWayland *
157 gst_vulkan_display_wayland_new (const gchar * name)
158 {
159 GstVulkanDisplayWayland *ret;
160 struct wl_display *display;
161 GType type;
162
163 /* get type first so we initialize the debug category */
164 type = GST_TYPE_VULKAN_DISPLAY_WAYLAND;
165 display = wl_display_connect (name);
166
167 if (!display) {
168 GST_ERROR ("Failed to open Wayland display connection with name, \'%s\'",
169 name);
170 return NULL;
171 }
172
173 ret = g_object_new (type, NULL);
174 gst_object_ref_sink (ret);
175 ret->display = display;
176
177 /* connecting the listeners after attaching the event source will race with
178 * the source and the source may eat an event that we're waiting for and
179 * deadlock */
180 _connect_listeners (ret);
181
182 GST_VULKAN_DISPLAY (ret)->event_source =
183 wayland_event_source_new (ret->display, NULL);
184 g_source_attach (GST_VULKAN_DISPLAY (ret)->event_source,
185 GST_VULKAN_DISPLAY (ret)->main_context);
186
187 return ret;
188 }
189
190 /**
191 * gst_vulkan_display_wayland_new_with_display:
192 * @display: an existing, wayland display
193 *
194 * Creates a new display connection from a wl_display Display.
195 *
196 * Returns: (transfer full): a new #GstVulkanDisplayWayland
197 *
198 * Since: 1.18
199 */
200 GstVulkanDisplayWayland *
201 gst_vulkan_display_wayland_new_with_display (struct wl_display *display)
202 {
203 GstVulkanDisplayWayland *ret;
204
205 g_return_val_if_fail (display != NULL, NULL);
206
207 ret = g_object_new (GST_TYPE_VULKAN_DISPLAY_WAYLAND, NULL);
208 gst_object_ref_sink (ret);
209
210 ret->display = display;
211 ret->foreign_display = TRUE;
212
213 _connect_listeners (ret);
214
215 return ret;
216 }
217
218 static gpointer
219 gst_vulkan_display_wayland_get_handle (GstVulkanDisplay * display)
220 {
221 return GST_VULKAN_DISPLAY_WAYLAND (display)->display;
222 }
223
224 static gboolean
225 _roundtrip_async (gpointer data)
226 {
227 GstVulkanDisplayWayland *display = data;
228
229 wl_display_roundtrip (display->display);
230
231 return G_SOURCE_REMOVE;
232 }
233
234 void
235 gst_vulkan_display_wayland_roundtrip_async (GstVulkanDisplayWayland * display)
236 {
237 g_return_if_fail (GST_IS_VULKAN_DISPLAY_WAYLAND (display));
238
239 g_main_context_invoke (GST_VULKAN_DISPLAY (display)->main_context,
240 (GSourceFunc) _roundtrip_async, display);
241 }