3 * Copyright (C) 2016 Matthew Waters <matthew@centricular.com>
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.
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.
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.
21 #define GLIB_DISABLE_DEPRECATION_WARNINGS
27 #include <linux/input.h>
29 #include <gst/vulkan/wayland/gstvkdisplay_wayland.h>
30 #include "gstvkwindow_wayland.h"
32 #include "wayland_event_source.h"
34 #define GST_CAT_DEFAULT gst_vulkan_window_wayland_debug
35 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
40 static gsize _init = 0;
42 if (g_once_init_enter (&_init)) {
43 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "vulkanwindowxcb", 0,
45 g_once_init_leave (&_init, 1);
49 #define gst_vulkan_window_wayland_parent_class parent_class
50 G_DEFINE_TYPE_WITH_CODE (GstVulkanWindowWayland, gst_vulkan_window_wayland,
51 GST_TYPE_VULKAN_WINDOW, _init_debug ());
53 static void gst_vulkan_window_wayland_close (GstVulkanWindow * window);
54 static gboolean gst_vulkan_window_wayland_open (GstVulkanWindow * window,
56 static VkSurfaceKHR gst_vulkan_window_wayland_get_surface (GstVulkanWindow
57 * window, GError ** error);
59 gst_vulkan_window_wayland_get_presentation_support (GstVulkanWindow *
60 window, GstVulkanDevice * device, guint32 queue_family_idx);
63 handle_ping (void *data, struct wl_shell_surface *shell_surface,
66 GstVulkanWindowWayland *window_wl = data;
68 GST_TRACE_OBJECT (window_wl, "ping received serial %u", serial);
70 wl_shell_surface_pong (shell_surface, serial);
74 static void window_resize (GstVulkanWindowWayland * window_wl, guint width,
78 handle_configure (void *data, struct wl_shell_surface *shell_surface,
79 uint32_t edges, int32_t width, int32_t height)
81 GstVulkanWindowWayland *window_wl = data;
83 GST_DEBUG_OBJECT (window_wl, "configure event on surface %p, %ix%i",
84 shell_surface, width, height);
86 /*window_resize (window_wl, width, height); */
90 handle_popup_done (void *data, struct wl_shell_surface *shell_surface)
94 static const struct wl_shell_surface_listener shell_surface_listener = {
101 destroy_surfaces (GstVulkanWindowWayland * window_wl)
103 GST_DEBUG_OBJECT (window_wl, "destroying created surfaces");
105 if (window_wl->shell_surface) {
106 wl_shell_surface_destroy (window_wl->shell_surface);
107 window_wl->shell_surface = NULL;
109 if (window_wl->surface) {
110 wl_surface_destroy (window_wl->surface);
111 window_wl->surface = NULL;
116 create_surfaces (GstVulkanWindowWayland * window_wl)
118 GstVulkanDisplayWayland *display =
119 GST_VULKAN_DISPLAY_WAYLAND (GST_VULKAN_WINDOW (window_wl)->display);
122 if (!window_wl->surface) {
123 window_wl->surface = wl_compositor_create_surface (display->compositor);
124 if (window_wl->queue)
125 wl_proxy_set_queue ((struct wl_proxy *) window_wl->surface,
129 if (!window_wl->shell_surface) {
130 window_wl->shell_surface =
131 wl_shell_get_shell_surface (display->shell, window_wl->surface);
132 if (window_wl->queue)
133 wl_proxy_set_queue ((struct wl_proxy *) window_wl->shell_surface,
136 wl_shell_surface_add_listener (window_wl->shell_surface,
137 &shell_surface_listener, window_wl);
139 wl_shell_surface_set_title (window_wl->shell_surface, "Vulkan Renderer");
140 wl_shell_surface_set_toplevel (window_wl->shell_surface);
141 GST_DEBUG_OBJECT (window_wl, "Successfully created shell surface %p",
142 window_wl->shell_surface);
145 if (window_wl->window_width > 0)
146 width = window_wl->window_width;
149 window_wl->window_width = width;
151 if (window_wl->window_height > 0)
152 height = window_wl->window_height;
155 window_wl->window_height = height;
157 gst_vulkan_window_resize (GST_VULKAN_WINDOW (window_wl),
158 window_wl->window_width, window_wl->window_height);
162 gst_vulkan_window_wayland_class_init (GstVulkanWindowWaylandClass * klass)
164 GstVulkanWindowClass *window_class = (GstVulkanWindowClass *) klass;
166 window_class->close = GST_DEBUG_FUNCPTR (gst_vulkan_window_wayland_close);
167 window_class->open = GST_DEBUG_FUNCPTR (gst_vulkan_window_wayland_open);
168 window_class->get_surface =
169 GST_DEBUG_FUNCPTR (gst_vulkan_window_wayland_get_surface);
170 window_class->get_presentation_support =
171 GST_DEBUG_FUNCPTR (gst_vulkan_window_wayland_get_presentation_support);
175 gst_vulkan_window_wayland_init (GstVulkanWindowWayland * window)
179 GstVulkanWindowWayland *
180 gst_vulkan_window_wayland_new (GstVulkanDisplay * display)
182 GstVulkanWindowWayland *window;
184 if ((gst_vulkan_display_get_handle_type (display) &
185 GST_VULKAN_DISPLAY_TYPE_WAYLAND)
187 /* we require a wayland display to create wayland surfaces */
192 GST_DEBUG ("creating Wayland window");
194 window = g_object_new (GST_TYPE_VULKAN_WINDOW_WAYLAND, NULL);
195 gst_object_ref_sink (window);
201 gst_vulkan_window_wayland_close (GstVulkanWindow * window)
203 GstVulkanWindowWayland *window_wl;
205 window_wl = GST_VULKAN_WINDOW_WAYLAND (window);
207 destroy_surfaces (window_wl);
209 g_source_destroy (window_wl->wl_source);
210 g_source_unref (window_wl->wl_source);
211 window_wl->wl_source = NULL;
213 GST_VULKAN_WINDOW_CLASS (parent_class)->close (window);
217 gst_vulkan_window_wayland_open (GstVulkanWindow * window, GError ** error)
219 GstVulkanDisplayWayland *display;
220 GstVulkanWindowWayland *window_wl = GST_VULKAN_WINDOW_WAYLAND (window);
222 if (!GST_IS_VULKAN_DISPLAY_WAYLAND (window->display)) {
223 g_set_error (error, GST_VULKAN_WINDOW_ERROR,
224 GST_VULKAN_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
225 "Failed to retrieve Wayland display (wrong type?)");
228 display = GST_VULKAN_DISPLAY_WAYLAND (window->display);
230 if (!display->display) {
231 g_set_error (error, GST_VULKAN_WINDOW_ERROR,
232 GST_VULKAN_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
233 "Failed to retrieve Wayland display");
237 window_wl->queue = NULL;
239 if (!GST_VULKAN_WINDOW_CLASS (parent_class)->open (window, error))
242 create_surfaces (window_wl);
244 gst_vulkan_display_wayland_roundtrip_async (display);
250 gst_vulkan_window_wayland_get_surface (GstVulkanWindow * window,
253 GstVulkanWindowWayland *window_wl = GST_VULKAN_WINDOW_WAYLAND (window);
254 VkWaylandSurfaceCreateInfoKHR info = { 0, };
258 info.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
261 info.display = GST_VULKAN_DISPLAY_WAYLAND_DISPLAY (window->display);
262 info.surface = window_wl->surface;
264 if (!window_wl->CreateWaylandSurface)
265 window_wl->CreateWaylandSurface =
266 gst_vulkan_instance_get_proc_address (window->display->instance,
267 "vkCreateWaylandSurfaceKHR");
268 if (!window_wl->CreateWaylandSurface) {
269 g_set_error_literal (error, GST_VULKAN_ERROR, VK_ERROR_FEATURE_NOT_PRESENT,
270 "Could not retrieve \"vkCreateWaylandSurfaceKHR\" function pointer");
275 window_wl->CreateWaylandSurface (window->display->instance->instance,
277 if (gst_vulkan_error_to_g_error (err, error, "vkCreateWaylandSurfaceKHR") < 0)
284 gst_vulkan_window_wayland_get_presentation_support (GstVulkanWindow * window,
285 GstVulkanDevice * device, guint32 queue_family_idx)
287 GstVulkanWindowWayland *window_wl = GST_VULKAN_WINDOW_WAYLAND (window);
288 VkPhysicalDevice gpu;
290 if (!window_wl->GetPhysicalDeviceWaylandPresentationSupport)
291 window_wl->GetPhysicalDeviceWaylandPresentationSupport =
292 gst_vulkan_instance_get_proc_address (window->display->instance,
293 "vkGetPhysicalDeviceWaylandPresentationSupportKHR");
294 if (!window_wl->GetPhysicalDeviceWaylandPresentationSupport) {
295 GST_WARNING_OBJECT (window, "Could not retrieve "
296 "\"vkGetPhysicalDeviceWaylandPresentationSupportKHR\" "
301 gpu = gst_vulkan_device_get_physical_device (device);
302 if (window_wl->GetPhysicalDeviceWaylandPresentationSupport (gpu,
304 GST_VULKAN_DISPLAY_WAYLAND_DISPLAY (window->display)))