Debian Codegraph @

gst-libs/gst/vulkan/wayland/gstvkwindow_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 #define GLIB_DISABLE_DEPRECATION_WARNINGS
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <linux/input.h>
28
29 #include <gst/vulkan/wayland/gstvkdisplay_wayland.h>
30 #include "gstvkwindow_wayland.h"
31 #include "gstvkdisplay_wayland_private.h"
32
33 #include "wayland_event_source.h"
34
35 #define GST_CAT_DEFAULT gst_vulkan_window_wayland_debug
36 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
37
38 static void
39 _init_debug (void)
40 {
41 static gsize _init = 0;
42
43 if (g_once_init_enter (&_init)) {
44 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "vulkanwindowwayland", 0,
45 "Vulkan Wayland Window");
46 g_once_init_leave (&_init, 1);
47 }
48 }
49
50 #define gst_vulkan_window_wayland_parent_class parent_class
51 G_DEFINE_TYPE_WITH_CODE (GstVulkanWindowWayland, gst_vulkan_window_wayland,
52 GST_TYPE_VULKAN_WINDOW, _init_debug ());
53
54 static void gst_vulkan_window_wayland_close (GstVulkanWindow * window);
55 static gboolean gst_vulkan_window_wayland_open (GstVulkanWindow * window,
56 GError ** error);
57 static VkSurfaceKHR gst_vulkan_window_wayland_get_surface (GstVulkanWindow
58 * window, GError ** error);
59 static gboolean
60 gst_vulkan_window_wayland_get_presentation_support (GstVulkanWindow *
61 window, GstVulkanDevice * device, guint32 queue_family_idx);
62
63 static void
64 handle_xdg_toplevel_close (void *data, struct xdg_toplevel *xdg_toplevel)
65 {
66 GstVulkanWindow *window = data;
67
68 GST_DEBUG ("XDG toplevel got a \"close\" event.");
69
70 gst_vulkan_window_close (window);
71 }
72
73 static void
74 handle_xdg_toplevel_configure (void *data, struct xdg_toplevel *xdg_toplevel,
75 int32_t width, int32_t height, struct wl_array *states)
76 {
77 #if 0
78 GstVulkanWindowWayland *window_wl = data;
79 const uint32_t *state;
80 #endif
81 GST_DEBUG ("configure event on XDG toplevel %p, %ix%i", xdg_toplevel,
82 width, height);
83 #if 0
84 // TODO: deal with resizes
85 if (width > 0 && height > 0)
86 window_resize (window_egl, width, height);
87 #endif
88 }
89
90 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
91 handle_xdg_toplevel_configure,
92 handle_xdg_toplevel_close,
93 };
94
95 static void
96 handle_xdg_surface_configure (void *data, struct xdg_surface *xdg_surface,
97 uint32_t serial)
98 {
99 xdg_surface_ack_configure (xdg_surface, serial);
100 }
101
102 static const struct xdg_surface_listener xdg_surface_listener = {
103 handle_xdg_surface_configure,
104 };
105
106 static void
107 create_xdg_surface (GstVulkanWindowWayland * window_wl)
108 {
109 GstVulkanDisplayWayland *display =
110 GST_VULKAN_DISPLAY_WAYLAND (GST_VULKAN_WINDOW (window_wl)->display);
111 GstVulkanDisplayWaylandPrivate *display_priv =
112 gst_vulkan_display_wayland_get_private (display);
113 struct xdg_surface *xdg_surface;
114 struct xdg_toplevel *xdg_toplevel;
115
116 GST_DEBUG ("Creating surfaces with XDG-shell");
117
118 /* First create the XDG surface */
119 xdg_surface = xdg_wm_base_get_xdg_surface (display_priv->xdg_wm_base,
120 window_wl->surface);
121 xdg_surface_add_listener (xdg_surface, &xdg_surface_listener, window_wl);
122
123 /* Then the XDG top-level */
124 xdg_toplevel = xdg_surface_get_toplevel (xdg_surface);
125 if (g_get_prgname ()) {
126 xdg_toplevel_set_app_id (xdg_toplevel, g_get_prgname ());
127 } else {
128 xdg_toplevel_set_app_id (xdg_toplevel, "org.gstreamer.wayland");
129 }
130 xdg_toplevel_set_title (xdg_toplevel, "Vulkan Renderer");
131 xdg_toplevel_add_listener (xdg_toplevel, &xdg_toplevel_listener, window_wl);
132
133 /* Commit the xdg_surface state */
134 wl_surface_commit (window_wl->surface);
135
136 /* And save them into the fields */
137 window_wl->xdg_surface = xdg_surface;
138 window_wl->xdg_toplevel = xdg_toplevel;
139 }
140
141 static void
142 handle_ping (void *data, struct wl_shell_surface *shell_surface,
143 uint32_t serial)
144 {
145 GstVulkanWindowWayland *window_wl = data;
146
147 GST_TRACE_OBJECT (window_wl, "ping received serial %u", serial);
148
149 wl_shell_surface_pong (shell_surface, serial);
150 }
151
152 /*
153 static void window_resize (GstVulkanWindowWayland * window_wl, guint width,
154 guint height);*/
155
156 static void
157 handle_configure (void *data, struct wl_shell_surface *shell_surface,
158 uint32_t edges, int32_t width, int32_t height)
159 {
160 GstVulkanWindowWayland *window_wl = data;
161
162 GST_DEBUG_OBJECT (window_wl, "configure event on surface %p, %ix%i",
163 shell_surface, width, height);
164
165 /*window_resize (window_wl, width, height); */
166 }
167
168 static void
169 handle_popup_done (void *data, struct wl_shell_surface *shell_surface)
170 {
171 }
172
173 static const struct wl_shell_surface_listener shell_surface_listener = {
174 handle_ping,
175 handle_configure,
176 handle_popup_done
177 };
178
179 static void
180 create_wl_shell_surface (GstVulkanWindowWayland * window_wl)
181 {
182 GstVulkanDisplayWayland *display =
183 GST_VULKAN_DISPLAY_WAYLAND (GST_VULKAN_WINDOW (window_wl)->display);
184
185 GST_DEBUG ("Creating surfaces with wl_shell");
186
187 window_wl->shell_surface =
188 wl_shell_get_shell_surface (display->shell, window_wl->surface);
189 if (window_wl->queue)
190 wl_proxy_set_queue ((struct wl_proxy *) window_wl->shell_surface,
191 window_wl->queue);
192
193 wl_shell_surface_add_listener (window_wl->shell_surface,
194 &shell_surface_listener, window_wl);
195
196 wl_shell_surface_set_title (window_wl->shell_surface, "Vulkan Renderer");
197 wl_shell_surface_set_toplevel (window_wl->shell_surface);
198 GST_DEBUG_OBJECT (window_wl, "Successfully created shell surface %p",
199 window_wl->shell_surface);
200 }
201
202 static void
203 destroy_surfaces (GstVulkanWindowWayland * window_wl)
204 {
205 GST_DEBUG_OBJECT (window_wl, "destroying created surfaces");
206
207 g_clear_pointer (&window_wl->xdg_toplevel, xdg_toplevel_destroy);
208 g_clear_pointer (&window_wl->xdg_surface, xdg_surface_destroy);
209 g_clear_pointer (&window_wl->shell_surface, wl_shell_surface_destroy);
210 g_clear_pointer (&window_wl->surface, wl_surface_destroy);
211 }
212
213 static void
214 create_surfaces (GstVulkanWindowWayland * window_wl)
215 {
216 GstVulkanDisplayWayland *display =
217 GST_VULKAN_DISPLAY_WAYLAND (GST_VULKAN_WINDOW (window_wl)->display);
218 GstVulkanDisplayWaylandPrivate *display_priv =
219 gst_vulkan_display_wayland_get_private (display);
220 gint width, height;
221
222 if (!window_wl->surface) {
223 window_wl->surface = wl_compositor_create_surface (display->compositor);
224 if (window_wl->queue)
225 wl_proxy_set_queue ((struct wl_proxy *) window_wl->surface,
226 window_wl->queue);
227 }
228
229 if (display_priv->xdg_wm_base) {
230 create_xdg_surface (window_wl);
231 } else {
232 create_wl_shell_surface (window_wl);
233 }
234
235 if (window_wl->window_width > 0)
236 width = window_wl->window_width;
237 else
238 width = 320;
239 window_wl->window_width = width;
240
241 if (window_wl->window_height > 0)
242 height = window_wl->window_height;
243 else
244 height = 240;
245 window_wl->window_height = height;
246
247 gst_vulkan_window_resize (GST_VULKAN_WINDOW (window_wl),
248 window_wl->window_width, window_wl->window_height);
249 }
250
251 static void
252 gst_vulkan_window_wayland_class_init (GstVulkanWindowWaylandClass * klass)
253 {
254 GstVulkanWindowClass *window_class = (GstVulkanWindowClass *) klass;
255
256 window_class->close = GST_DEBUG_FUNCPTR (gst_vulkan_window_wayland_close);
257 window_class->open = GST_DEBUG_FUNCPTR (gst_vulkan_window_wayland_open);
258 window_class->get_surface =
259 GST_DEBUG_FUNCPTR (gst_vulkan_window_wayland_get_surface);
260 window_class->get_presentation_support =
261 GST_DEBUG_FUNCPTR (gst_vulkan_window_wayland_get_presentation_support);
262 }
263
264 static void
265 gst_vulkan_window_wayland_init (GstVulkanWindowWayland * window)
266 {
267 }
268
269 GstVulkanWindowWayland *
270 gst_vulkan_window_wayland_new (GstVulkanDisplay * display)
271 {
272 GstVulkanWindowWayland *window;
273
274 if ((gst_vulkan_display_get_handle_type (display) &
275 GST_VULKAN_DISPLAY_TYPE_WAYLAND)
276 == 0)
277 /* we require a wayland display to create wayland surfaces */
278 return NULL;
279
280 _init_debug ();
281
282 GST_DEBUG ("creating Wayland window");
283
284 window = g_object_new (GST_TYPE_VULKAN_WINDOW_WAYLAND, NULL);
285 gst_object_ref_sink (window);
286
287 return window;
288 }
289
290 static void
291 gst_vulkan_window_wayland_close (GstVulkanWindow * window)
292 {
293 GstVulkanWindowWayland *window_wl;
294
295 window_wl = GST_VULKAN_WINDOW_WAYLAND (window);
296
297 destroy_surfaces (window_wl);
298
299 g_source_destroy (window_wl->wl_source);
300 g_source_unref (window_wl->wl_source);
301 window_wl->wl_source = NULL;
302
303 GST_VULKAN_WINDOW_CLASS (parent_class)->close (window);
304 }
305
306 static gboolean
307 gst_vulkan_window_wayland_open (GstVulkanWindow * window, GError ** error)
308 {
309 GstVulkanDisplayWayland *display;
310 GstVulkanWindowWayland *window_wl = GST_VULKAN_WINDOW_WAYLAND (window);
311
312 if (!GST_IS_VULKAN_DISPLAY_WAYLAND (window->display)) {
313 g_set_error (error, GST_VULKAN_WINDOW_ERROR,
314 GST_VULKAN_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
315 "Failed to retrieve Wayland display (wrong type?)");
316 return FALSE;
317 }
318 display = GST_VULKAN_DISPLAY_WAYLAND (window->display);
319
320 if (!display->display) {
321 g_set_error (error, GST_VULKAN_WINDOW_ERROR,
322 GST_VULKAN_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
323 "Failed to retrieve Wayland display");
324 return FALSE;
325 }
326
327 window_wl->queue = NULL;
328
329 if (!GST_VULKAN_WINDOW_CLASS (parent_class)->open (window, error))
330 return FALSE;
331
332 create_surfaces (window_wl);
333
334 gst_vulkan_display_wayland_roundtrip_async (display);
335
336 return TRUE;
337 }
338
339 static VkSurfaceKHR
340 gst_vulkan_window_wayland_get_surface (GstVulkanWindow * window,
341 GError ** error)
342 {
343 GstVulkanWindowWayland *window_wl = GST_VULKAN_WINDOW_WAYLAND (window);
344 VkWaylandSurfaceCreateInfoKHR info = { 0, };
345 VkSurfaceKHR ret;
346 VkResult err;
347
348 info.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
349 info.pNext = NULL;
350 info.flags = 0;
351 info.display = GST_VULKAN_DISPLAY_WAYLAND_DISPLAY (window->display);
352 info.surface = window_wl->surface;
353
354 if (!window_wl->CreateWaylandSurface)
355 window_wl->CreateWaylandSurface =
356 gst_vulkan_instance_get_proc_address (window->display->instance,
357 "vkCreateWaylandSurfaceKHR");
358 if (!window_wl->CreateWaylandSurface) {
359 g_set_error_literal (error, GST_VULKAN_ERROR, VK_ERROR_FEATURE_NOT_PRESENT,
360 "Could not retrieve \"vkCreateWaylandSurfaceKHR\" function pointer");
361 return VK_NULL_HANDLE;
362 }
363
364 err =
365 window_wl->CreateWaylandSurface (window->display->instance->instance,
366 &info, NULL, &ret);
367 if (gst_vulkan_error_to_g_error (err, error, "vkCreateWaylandSurfaceKHR") < 0)
368 return VK_NULL_HANDLE;
369
370 return ret;
371 }
372
373 static gboolean
374 gst_vulkan_window_wayland_get_presentation_support (GstVulkanWindow * window,
375 GstVulkanDevice * device, guint32 queue_family_idx)
376 {
377 GstVulkanWindowWayland *window_wl = GST_VULKAN_WINDOW_WAYLAND (window);
378 VkPhysicalDevice gpu;
379
380 if (!window_wl->GetPhysicalDeviceWaylandPresentationSupport)
381 window_wl->GetPhysicalDeviceWaylandPresentationSupport =
382 gst_vulkan_instance_get_proc_address (window->display->instance,
383 "vkGetPhysicalDeviceWaylandPresentationSupportKHR");
384 if (!window_wl->GetPhysicalDeviceWaylandPresentationSupport) {
385 GST_WARNING_OBJECT (window, "Could not retrieve "
386 "\"vkGetPhysicalDeviceWaylandPresentationSupportKHR\" "
387 "function pointer");
388 return FALSE;
389 }
390
391 gpu = gst_vulkan_device_get_physical_device (device);
392 if (window_wl->GetPhysicalDeviceWaylandPresentationSupport (gpu,
393 queue_family_idx,
394 GST_VULKAN_DISPLAY_WAYLAND_DISPLAY (window->display)))
395 return TRUE;
396 return FALSE;
397 }