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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include "wayland/vkdisplay_wayland.h"
26 #include "wayland_event_source.h"
27 
28 GST_DEBUG_CATEGORY_STATIC (gst_vulkan_display_wayland_debug);
29 #define GST_CAT_DEFAULT gst_vulkan_display_wayland_debug
30 
31 G_DEFINE_TYPE_WITH_CODE (GstVulkanDisplayWayland, gst_vulkan_display_wayland,
32     GST_TYPE_VULKAN_DISPLAY, GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT,
33         "vulkandisplaywayland", 0, "Vulkan Wayland Display");
34     );
35 
36 static void gst_vulkan_display_wayland_finalize (GObject * object);
37 static gpointer gst_vulkan_display_wayland_get_handle (GstVulkanDisplay *
38     display);
39 
40 static void
registry_handle_global(void * data,struct wl_registry * registry,uint32_t name,const char * interface,uint32_t version)41 registry_handle_global (void *data, struct wl_registry *registry,
42     uint32_t name, const char *interface, uint32_t version)
43 {
44   GstVulkanDisplayWayland *display = data;
45 
46   GST_TRACE_OBJECT (display, "registry_handle_global with registry %p, "
47       "interface %s, version %u", registry, interface, version);
48 
49   if (g_strcmp0 (interface, "wl_compositor") == 0) {
50     display->compositor =
51         wl_registry_bind (registry, name, &wl_compositor_interface, 1);
52   } else if (g_strcmp0 (interface, "wl_subcompositor") == 0) {
53     display->subcompositor =
54         wl_registry_bind (registry, name, &wl_subcompositor_interface, 1);
55   } else if (g_strcmp0 (interface, "wl_shell") == 0) {
56     display->shell = wl_registry_bind (registry, name, &wl_shell_interface, 1);
57   }
58 }
59 
60 static const struct wl_registry_listener registry_listener = {
61   registry_handle_global
62 };
63 
64 static void
_connect_listeners(GstVulkanDisplayWayland * display)65 _connect_listeners (GstVulkanDisplayWayland * display)
66 {
67   display->registry = wl_display_get_registry (display->display);
68   wl_registry_add_listener (display->registry, &registry_listener, display);
69 
70   wl_display_roundtrip (display->display);
71 }
72 
73 static void
gst_vulkan_display_wayland_class_init(GstVulkanDisplayWaylandClass * klass)74 gst_vulkan_display_wayland_class_init (GstVulkanDisplayWaylandClass * klass)
75 {
76   GST_VULKAN_DISPLAY_CLASS (klass)->get_handle =
77       GST_DEBUG_FUNCPTR (gst_vulkan_display_wayland_get_handle);
78 
79   G_OBJECT_CLASS (klass)->finalize = gst_vulkan_display_wayland_finalize;
80 }
81 
82 static void
gst_vulkan_display_wayland_init(GstVulkanDisplayWayland * display_wayland)83 gst_vulkan_display_wayland_init (GstVulkanDisplayWayland * display_wayland)
84 {
85   GstVulkanDisplay *display = (GstVulkanDisplay *) display_wayland;
86 
87   display->type = GST_VULKAN_DISPLAY_TYPE_WAYLAND;
88   display_wayland->foreign_display = FALSE;
89 }
90 
91 static void
gst_vulkan_display_wayland_finalize(GObject * object)92 gst_vulkan_display_wayland_finalize (GObject * object)
93 {
94   GstVulkanDisplayWayland *display_wayland =
95       GST_VULKAN_DISPLAY_WAYLAND (object);
96 
97   if (!display_wayland->foreign_display && display_wayland->display) {
98     wl_display_flush (display_wayland->display);
99     wl_display_disconnect (display_wayland->display);
100   }
101 
102   G_OBJECT_CLASS (gst_vulkan_display_wayland_parent_class)->finalize (object);
103 }
104 
105 /**
106  * gst_vulkan_display_wayland_new:
107  * @name: (allow-none): a display name
108  *
109  * Create a new #GstVulkanDisplayWayland from the wayland display name.  See wl_display_connect()
110  * for details on what is a valid name.
111  *
112  * Returns: (transfer full): a new #GstVulkanDisplayWayland or %NULL
113  */
114 GstVulkanDisplayWayland *
gst_vulkan_display_wayland_new(const gchar * name)115 gst_vulkan_display_wayland_new (const gchar * name)
116 {
117   GstVulkanDisplayWayland *ret;
118 
119   ret = g_object_new (GST_TYPE_VULKAN_DISPLAY_WAYLAND, NULL);
120   gst_object_ref_sink (ret);
121   ret->display = wl_display_connect (name);
122 
123   if (!ret->display) {
124     GST_ERROR ("Failed to open Wayland display connection with name, \'%s\'",
125         name);
126     return NULL;
127   }
128 
129   /* connecting the listeners after attaching the event source will race with
130    * the source and the source may eat an event that we're waiting for and
131    * deadlock */
132   _connect_listeners (ret);
133 
134   GST_VULKAN_DISPLAY (ret)->event_source =
135       wayland_event_source_new (ret->display, NULL);
136   g_source_attach (GST_VULKAN_DISPLAY (ret)->event_source,
137       GST_VULKAN_DISPLAY (ret)->main_context);
138 
139   return ret;
140 }
141 
142 /**
143  * gst_vulkan_display_wayland_new_with_display:
144  * @display: an existing, wayland display
145  *
146  * Creates a new display connection from a wl_display Display.
147  *
148  * Returns: (transfer full): a new #GstVulkanDisplayWayland
149  */
150 GstVulkanDisplayWayland *
gst_vulkan_display_wayland_new_with_display(struct wl_display * display)151 gst_vulkan_display_wayland_new_with_display (struct wl_display * display)
152 {
153   GstVulkanDisplayWayland *ret;
154 
155   g_return_val_if_fail (display != NULL, NULL);
156 
157   ret = g_object_new (GST_TYPE_VULKAN_DISPLAY_WAYLAND, NULL);
158   gst_object_ref_sink (ret);
159 
160   ret->display = display;
161   ret->foreign_display = TRUE;
162 
163   _connect_listeners (ret);
164 
165   return ret;
166 }
167 
168 static gpointer
gst_vulkan_display_wayland_get_handle(GstVulkanDisplay * display)169 gst_vulkan_display_wayland_get_handle (GstVulkanDisplay * display)
170 {
171   return GST_VULKAN_DISPLAY_WAYLAND (display)->display;
172 }
173 
174 static gboolean
_roundtrip_async(gpointer data)175 _roundtrip_async (gpointer data)
176 {
177   GstVulkanDisplayWayland *display = data;
178 
179   wl_display_roundtrip (display->display);
180 
181   return G_SOURCE_REMOVE;
182 }
183 
184 void
gst_vulkan_display_wayland_roundtrip_async(GstVulkanDisplayWayland * display)185 gst_vulkan_display_wayland_roundtrip_async (GstVulkanDisplayWayland * display)
186 {
187   g_return_if_fail (GST_IS_VULKAN_DISPLAY_WAYLAND (display));
188 
189   g_main_context_invoke (GST_VULKAN_DISPLAY (display)->main_context,
190       (GSourceFunc) _roundtrip_async, display);
191 }
192