• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GStreamer
3  * Copyright (C) 2016 Matthew Waters <matthew@centricular.com>
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 
32 #include "wayland_event_source.h"
33 
34 #define GST_CAT_DEFAULT gst_vulkan_window_wayland_debug
35 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
36 
37 static void
_init_debug(void)38 _init_debug (void)
39 {
40   static gsize _init = 0;
41 
42   if (g_once_init_enter (&_init)) {
43     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "vulkanwindowxcb", 0,
44         "Vulkan XCB Window");
45     g_once_init_leave (&_init, 1);
46   }
47 }
48 
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 ());
52 
53 static void gst_vulkan_window_wayland_close (GstVulkanWindow * window);
54 static gboolean gst_vulkan_window_wayland_open (GstVulkanWindow * window,
55     GError ** error);
56 static VkSurfaceKHR gst_vulkan_window_wayland_get_surface (GstVulkanWindow
57     * window, GError ** error);
58 static gboolean
59 gst_vulkan_window_wayland_get_presentation_support (GstVulkanWindow *
60     window, GstVulkanDevice * device, guint32 queue_family_idx);
61 
62 static void
handle_ping(void * data,struct wl_shell_surface * shell_surface,uint32_t serial)63 handle_ping (void *data, struct wl_shell_surface *shell_surface,
64     uint32_t serial)
65 {
66   GstVulkanWindowWayland *window_wl = data;
67 
68   GST_TRACE_OBJECT (window_wl, "ping received serial %u", serial);
69 
70   wl_shell_surface_pong (shell_surface, serial);
71 }
72 
73 /*
74 static void window_resize (GstVulkanWindowWayland * window_wl, guint width,
75     guint height);*/
76 
77 static void
handle_configure(void * data,struct wl_shell_surface * shell_surface,uint32_t edges,int32_t width,int32_t height)78 handle_configure (void *data, struct wl_shell_surface *shell_surface,
79     uint32_t edges, int32_t width, int32_t height)
80 {
81   GstVulkanWindowWayland *window_wl = data;
82 
83   GST_DEBUG_OBJECT (window_wl, "configure event on surface %p, %ix%i",
84       shell_surface, width, height);
85 
86   /*window_resize (window_wl, width, height); */
87 }
88 
89 static void
handle_popup_done(void * data,struct wl_shell_surface * shell_surface)90 handle_popup_done (void *data, struct wl_shell_surface *shell_surface)
91 {
92 }
93 
94 static const struct wl_shell_surface_listener shell_surface_listener = {
95   handle_ping,
96   handle_configure,
97   handle_popup_done
98 };
99 
100 static void
destroy_surfaces(GstVulkanWindowWayland * window_wl)101 destroy_surfaces (GstVulkanWindowWayland * window_wl)
102 {
103   GST_DEBUG_OBJECT (window_wl, "destroying created surfaces");
104 
105   if (window_wl->shell_surface) {
106     wl_shell_surface_destroy (window_wl->shell_surface);
107     window_wl->shell_surface = NULL;
108   }
109   if (window_wl->surface) {
110     wl_surface_destroy (window_wl->surface);
111     window_wl->surface = NULL;
112   }
113 }
114 
115 static void
create_surfaces(GstVulkanWindowWayland * window_wl)116 create_surfaces (GstVulkanWindowWayland * window_wl)
117 {
118   GstVulkanDisplayWayland *display =
119       GST_VULKAN_DISPLAY_WAYLAND (GST_VULKAN_WINDOW (window_wl)->display);
120   gint width, height;
121 
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,
126           window_wl->queue);
127   }
128 
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,
134           window_wl->queue);
135 
136     wl_shell_surface_add_listener (window_wl->shell_surface,
137         &shell_surface_listener, window_wl);
138 
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);
143   }
144 
145   if (window_wl->window_width > 0)
146     width = window_wl->window_width;
147   else
148     width = 320;
149   window_wl->window_width = width;
150 
151   if (window_wl->window_height > 0)
152     height = window_wl->window_height;
153   else
154     height = 240;
155   window_wl->window_height = height;
156 
157   gst_vulkan_window_resize (GST_VULKAN_WINDOW (window_wl),
158       window_wl->window_width, window_wl->window_height);
159 }
160 
161 static void
gst_vulkan_window_wayland_class_init(GstVulkanWindowWaylandClass * klass)162 gst_vulkan_window_wayland_class_init (GstVulkanWindowWaylandClass * klass)
163 {
164   GstVulkanWindowClass *window_class = (GstVulkanWindowClass *) klass;
165 
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);
172 }
173 
174 static void
gst_vulkan_window_wayland_init(GstVulkanWindowWayland * window)175 gst_vulkan_window_wayland_init (GstVulkanWindowWayland * window)
176 {
177 }
178 
179 GstVulkanWindowWayland *
gst_vulkan_window_wayland_new(GstVulkanDisplay * display)180 gst_vulkan_window_wayland_new (GstVulkanDisplay * display)
181 {
182   GstVulkanWindowWayland *window;
183 
184   if ((gst_vulkan_display_get_handle_type (display) &
185           GST_VULKAN_DISPLAY_TYPE_WAYLAND)
186       == 0)
187     /* we require a wayland display to create wayland surfaces */
188     return NULL;
189 
190   _init_debug ();
191 
192   GST_DEBUG ("creating Wayland window");
193 
194   window = g_object_new (GST_TYPE_VULKAN_WINDOW_WAYLAND, NULL);
195   gst_object_ref_sink (window);
196 
197   return window;
198 }
199 
200 static void
gst_vulkan_window_wayland_close(GstVulkanWindow * window)201 gst_vulkan_window_wayland_close (GstVulkanWindow * window)
202 {
203   GstVulkanWindowWayland *window_wl;
204 
205   window_wl = GST_VULKAN_WINDOW_WAYLAND (window);
206 
207   destroy_surfaces (window_wl);
208 
209   g_source_destroy (window_wl->wl_source);
210   g_source_unref (window_wl->wl_source);
211   window_wl->wl_source = NULL;
212 
213   GST_VULKAN_WINDOW_CLASS (parent_class)->close (window);
214 }
215 
216 static gboolean
gst_vulkan_window_wayland_open(GstVulkanWindow * window,GError ** error)217 gst_vulkan_window_wayland_open (GstVulkanWindow * window, GError ** error)
218 {
219   GstVulkanDisplayWayland *display;
220   GstVulkanWindowWayland *window_wl = GST_VULKAN_WINDOW_WAYLAND (window);
221 
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?)");
226     return FALSE;
227   }
228   display = GST_VULKAN_DISPLAY_WAYLAND (window->display);
229 
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");
234     return FALSE;
235   }
236 
237   window_wl->queue = NULL;
238 
239   if (!GST_VULKAN_WINDOW_CLASS (parent_class)->open (window, error))
240     return FALSE;
241 
242   create_surfaces (window_wl);
243 
244   gst_vulkan_display_wayland_roundtrip_async (display);
245 
246   return TRUE;
247 }
248 
249 static VkSurfaceKHR
gst_vulkan_window_wayland_get_surface(GstVulkanWindow * window,GError ** error)250 gst_vulkan_window_wayland_get_surface (GstVulkanWindow * window,
251     GError ** error)
252 {
253   GstVulkanWindowWayland *window_wl = GST_VULKAN_WINDOW_WAYLAND (window);
254   VkWaylandSurfaceCreateInfoKHR info = { 0, };
255   VkSurfaceKHR ret;
256   VkResult err;
257 
258   info.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
259   info.pNext = NULL;
260   info.flags = 0;
261   info.display = GST_VULKAN_DISPLAY_WAYLAND_DISPLAY (window->display);
262   info.surface = window_wl->surface;
263 
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");
271     return VK_NULL_HANDLE;
272   }
273 
274   err =
275       window_wl->CreateWaylandSurface (window->display->instance->instance,
276       &info, NULL, &ret);
277   if (gst_vulkan_error_to_g_error (err, error, "vkCreateWaylandSurfaceKHR") < 0)
278     return VK_NULL_HANDLE;
279 
280   return ret;
281 }
282 
283 static gboolean
gst_vulkan_window_wayland_get_presentation_support(GstVulkanWindow * window,GstVulkanDevice * device,guint32 queue_family_idx)284 gst_vulkan_window_wayland_get_presentation_support (GstVulkanWindow * window,
285     GstVulkanDevice * device, guint32 queue_family_idx)
286 {
287   GstVulkanWindowWayland *window_wl = GST_VULKAN_WINDOW_WAYLAND (window);
288   VkPhysicalDevice gpu;
289 
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\" "
297         "function pointer");
298     return FALSE;
299   }
300 
301   gpu = gst_vulkan_device_get_physical_device (device);
302   if (window_wl->GetPhysicalDeviceWaylandPresentationSupport (gpu,
303           queue_family_idx,
304           GST_VULKAN_DISPLAY_WAYLAND_DISPLAY (window->display)))
305     return TRUE;
306   return FALSE;
307 }
308