3 * Copyright (C) 2015 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.
25 #include "gstvkdisplay.h"
27 #if GST_VULKAN_HAVE_WINDOW_XCB
28 #include "xcb/gstvkdisplay_xcb.h"
30 #if GST_VULKAN_HAVE_WINDOW_WAYLAND
31 #include "wayland/gstvkdisplay_wayland.h"
33 #if GST_VULKAN_HAVE_WINDOW_COCOA
34 #include "cocoa/gstvkdisplay_cocoa.h"
36 #if GST_VULKAN_HAVE_WINDOW_IOS
37 #include "ios/gstvkdisplay_ios.h"
39 #if GST_VULKAN_HAVE_WINDOW_WIN32
40 #include "win32/gstvkwindow_win32.h"
42 #if GST_VULKAN_HAVE_WINDOW_ANDROID
43 #include "android/gstvkdisplay_android.h"
48 * @title: GstVulkanDisplay
49 * @short_description: window system display
50 * @see_also: #GstVulkanInstance, #GstVulkanWindow
52 * A #GstVulkanDisplay represents a connection to a display server on the platform
55 GST_DEBUG_CATEGORY_STATIC (GST_CAT_CONTEXT);
56 #define GST_CAT_DEFAULT gst_vulkan_display_debug
57 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
62 static volatile gsize _init = 0;
64 if (g_once_init_enter (&_init)) {
65 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "vulkandisplay", 0,
67 GST_DEBUG_CATEGORY_GET (GST_CAT_CONTEXT, "GST_CONTEXT");
68 g_once_init_leave (&_init, 1);
79 /* static guint gst_vulkan_display_signals[LAST_SIGNAL] = { 0 }; */
81 static void gst_vulkan_display_finalize (GObject * object);
82 static gpointer gst_vulkan_display_default_get_handle (GstVulkanDisplay *
84 static GstVulkanWindow
85 * gst_vulkan_display_default_create_window (GstVulkanDisplay * display);
87 struct _GstVulkanDisplayPrivate
89 GThread *event_thread;
95 #define GET_PRIV(display) gst_vulkan_display_get_instance_private (display)
97 G_DEFINE_TYPE_WITH_CODE (GstVulkanDisplay, gst_vulkan_display, GST_TYPE_OBJECT,
98 G_ADD_PRIVATE (GstVulkanDisplay) _init_debug ());
101 _event_thread_main (GstVulkanDisplay * display)
103 GstVulkanDisplayPrivate *priv = GET_PRIV (display);
105 g_mutex_lock (&priv->thread_lock);
107 display->main_context = g_main_context_new ();
108 display->main_loop = g_main_loop_new (display->main_context, FALSE);
110 g_cond_broadcast (&priv->thread_cond);
111 g_mutex_unlock (&priv->thread_lock);
113 g_main_loop_run (display->main_loop);
115 g_mutex_lock (&priv->thread_lock);
117 g_main_loop_unref (display->main_loop);
118 g_main_context_unref (display->main_context);
120 display->main_loop = NULL;
121 display->main_context = NULL;
123 g_cond_broadcast (&priv->thread_cond);
124 g_mutex_unlock (&priv->thread_lock);
130 gst_vulkan_display_class_init (GstVulkanDisplayClass * klass)
132 klass->get_handle = gst_vulkan_display_default_get_handle;
133 klass->create_window = gst_vulkan_display_default_create_window;
135 G_OBJECT_CLASS (klass)->finalize = gst_vulkan_display_finalize;
139 gst_vulkan_display_init (GstVulkanDisplay * display)
141 GstVulkanDisplayPrivate *priv = GET_PRIV (display);
143 display->type = GST_VULKAN_DISPLAY_TYPE_ANY;
145 g_mutex_init (&priv->thread_lock);
146 g_cond_init (&priv->thread_cond);
148 priv->event_thread = g_thread_new ("vkdisplay-event",
149 (GThreadFunc) _event_thread_main, display);
151 g_mutex_lock (&priv->thread_lock);
152 while (!display->main_loop)
153 g_cond_wait (&priv->thread_cond, &priv->thread_lock);
154 g_mutex_unlock (&priv->thread_lock);
158 free_window_weak_ref (GWeakRef * ref)
163 g_weak_ref_clear (ref);
168 gst_vulkan_display_finalize (GObject * object)
170 GstVulkanDisplay *display = GST_VULKAN_DISPLAY (object);
171 GstVulkanDisplayPrivate *priv = GET_PRIV (display);
173 g_mutex_lock (&priv->thread_lock);
175 if (display->main_loop)
176 g_main_loop_quit (display->main_loop);
178 while (display->main_loop)
179 g_cond_wait (&priv->thread_cond, &priv->thread_lock);
181 if (priv->event_thread)
182 g_thread_unref (priv->event_thread);
183 priv->event_thread = NULL;
184 g_mutex_unlock (&priv->thread_lock);
186 if (display->event_source) {
187 g_source_destroy (display->event_source);
188 g_source_unref (display->event_source);
190 display->event_source = NULL;
192 GST_OBJECT_LOCK (display);
193 g_list_free_full (display->windows, (GDestroyNotify) free_window_weak_ref);
194 display->windows = NULL;
195 GST_OBJECT_UNLOCK (display);
197 if (display->instance) {
198 gst_object_unref (display->instance);
201 G_OBJECT_CLASS (gst_vulkan_display_parent_class)->finalize (object);
205 * gst_vulkan_display_new_with_type:
206 * @instance: a #GstVulkanInstance
207 * @type: the #GstVulkanDisplayType to create
209 * Returns: (transfer full): a new #GstVulkanDisplay or %NULL if e.g. @type is
215 gst_vulkan_display_new_with_type (GstVulkanInstance * instance,
216 GstVulkanDisplayType type)
218 GstVulkanDisplay *display = NULL;
222 #if GST_VULKAN_HAVE_WINDOW_XCB
223 if (!display && type & GST_VULKAN_DISPLAY_TYPE_XCB) {
224 display = GST_VULKAN_DISPLAY (gst_vulkan_display_xcb_new (NULL));
227 #if GST_VULKAN_HAVE_WINDOW_WAYLAND
228 if (!display && type & GST_VULKAN_DISPLAY_TYPE_WAYLAND) {
229 display = GST_VULKAN_DISPLAY (gst_vulkan_display_wayland_new (NULL));
232 #if GST_VULKAN_HAVE_WINDOW_COCOA
233 if (!display && type & GST_VULKAN_DISPLAY_TYPE_COCOA) {
234 display = GST_VULKAN_DISPLAY (gst_vulkan_display_cocoa_new ());
237 #if GST_VULKAN_HAVE_WINDOW_IOS
238 if (!display && type & GST_VULKAN_DISPLAY_TYPE_IOS) {
239 display = GST_VULKAN_DISPLAY (gst_vulkan_display_ios_new ());
242 #if GST_VULKAN_HAVE_WINDOW_ANDROID
243 if (!display && type & GST_VULKAN_DISPLAY_TYPE_ANDROID) {
244 display = GST_VULKAN_DISPLAY (gst_vulkan_display_android_new ());
249 display->instance = gst_object_ref (instance);
255 * gst_vulkan_display_new:
257 * Returns: (transfer full): a new #GstVulkanDisplay
262 gst_vulkan_display_new (GstVulkanInstance * instance)
264 GstVulkanDisplayType type;
265 GstVulkanDisplay *display = NULL;
267 type = gst_vulkan_display_choose_type (instance);
268 display = gst_vulkan_display_new_with_type (instance, type);
271 /* subclass returned a NULL display */
272 GST_FIXME ("creating dummy display");
274 display = g_object_new (GST_TYPE_VULKAN_DISPLAY, NULL);
275 gst_object_ref_sink (display);
276 display->instance = gst_object_ref (instance);
283 * gst_vulkan_display_get_handle:
284 * @display: a #GstVulkanDisplay
286 * Returns: the winsys specific handle of @display
291 gst_vulkan_display_get_handle (GstVulkanDisplay * display)
293 GstVulkanDisplayClass *klass;
295 g_return_val_if_fail (GST_IS_VULKAN_DISPLAY (display), NULL);
296 klass = GST_VULKAN_DISPLAY_GET_CLASS (display);
297 g_return_val_if_fail (klass->get_handle != NULL, NULL);
299 return klass->get_handle (display);
303 gst_vulkan_display_default_get_handle (GstVulkanDisplay * display)
309 * gst_vulkan_display_get_handle_type:
310 * @display: a #GstVulkanDisplay
312 * Returns: the #GstVulkanDisplayType of @display
317 gst_vulkan_display_get_handle_type (GstVulkanDisplay * display)
319 g_return_val_if_fail (GST_IS_VULKAN_DISPLAY (display),
320 GST_VULKAN_DISPLAY_TYPE_NONE);
322 return display->type;
326 * gst_vulkan_display_create_window:
327 * @display: a #GstVulkanDisplay
329 * Returns: (transfer full): a new #GstVulkanWindow for @display or %NULL.
334 gst_vulkan_display_create_window (GstVulkanDisplay * display)
336 GstVulkanDisplayClass *klass;
337 GstVulkanWindow *window;
339 g_return_val_if_fail (GST_IS_VULKAN_DISPLAY (display), NULL);
340 klass = GST_VULKAN_DISPLAY_GET_CLASS (display);
341 g_return_val_if_fail (klass->create_window != NULL, NULL);
343 window = klass->create_window (display);
346 GWeakRef *ref = g_new0 (GWeakRef, 1);
348 g_weak_ref_set (ref, window);
350 GST_OBJECT_LOCK (display);
351 display->windows = g_list_prepend (display->windows, ref);
352 GST_OBJECT_UNLOCK (display);
358 static GstVulkanWindow *
359 gst_vulkan_display_default_create_window (GstVulkanDisplay * display)
361 return gst_vulkan_window_new (display);
365 _compare_vulkan_window (GWeakRef * ref, GstVulkanWindow * window)
367 GstVulkanWindow *other = g_weak_ref_get (ref);
368 gboolean equal = window == other;
371 gst_object_unref (other);
377 prepend_window_weakref_item (GWeakRef * ref, GList ** new)
379 GstVulkanWindow *window = g_weak_ref_get (ref);
381 *new = g_list_prepend (*new, window);
386 window_weak_list_to_strong (GstVulkanDisplay * display)
389 g_list_foreach (display->windows, (GFunc) prepend_window_weakref_item, &new);
394 * gst_vulkan_display_find_window:
395 * @display: a #GstVulkanDisplay
396 * @data: (closure): some data to pass to @compare_func
397 * @compare_func: (scope call): a comparison function to run
399 * Execute @compare_func over the list of windows stored by @display. The
400 * first argument to @compare_func is the #GstVulkanWindow being checked and the
401 * second argument is @data.
403 * Returns: (transfer full): The first #GstVulkanWindow that causes a match
409 gst_vulkan_display_find_window (GstVulkanDisplay * display, gpointer data,
410 GCompareFunc compare_func)
412 GstVulkanWindow *ret = NULL;
415 GST_OBJECT_LOCK (display);
416 windows = window_weak_list_to_strong (display);
417 l = g_list_find_custom (windows, data, (GCompareFunc) compare_func);
419 ret = gst_object_ref (l->data);
421 GST_DEBUG_OBJECT (display, "Found window %" GST_PTR_FORMAT
422 " (%p) in internal list", ret, ret);
423 GST_OBJECT_UNLOCK (display);
425 g_list_free_full (windows, gst_object_unref);
431 * gst_vulkan_display_remove_window:
432 * @display: a #GstVulkanDisplay
433 * @window: the #GstVulkanWindow to remove
435 * Returns: whether the window was successfully removed
440 gst_vulkan_display_remove_window (GstVulkanDisplay * display,
441 GstVulkanWindow * window)
443 gboolean ret = FALSE;
446 GST_OBJECT_LOCK (display);
447 l = g_list_find_custom (display->windows, window,
448 (GCompareFunc) _compare_vulkan_window);
450 GWeakRef *ref = l->data;
451 display->windows = g_list_delete_link (display->windows, l);
452 free_window_weak_ref (ref);
455 GST_OBJECT_UNLOCK (display);
461 * gst_context_set_vulkan_display:
462 * @context: a #GstContext
463 * @display: a #GstVulkanDisplay
465 * Sets @display on @context
470 gst_context_set_vulkan_display (GstContext * context,
471 GstVulkanDisplay * display)
475 g_return_if_fail (context != NULL);
476 g_return_if_fail (gst_context_is_writable (context));
479 GST_CAT_LOG (GST_CAT_CONTEXT,
480 "setting GstVulkanDisplay(%" GST_PTR_FORMAT ") on context(%"
481 GST_PTR_FORMAT ")", display, context);
483 s = gst_context_writable_structure (context);
484 gst_structure_set (s, GST_VULKAN_DISPLAY_CONTEXT_TYPE_STR,
485 GST_TYPE_VULKAN_DISPLAY, display, NULL);
489 * gst_context_get_vulkan_display:
490 * @context: a #GstContext
491 * @display: resulting #GstVulkanDisplay
493 * Returns: Whether @display was in @context
498 gst_context_get_vulkan_display (GstContext * context,
499 GstVulkanDisplay ** display)
501 const GstStructure *s;
504 g_return_val_if_fail (display != NULL, FALSE);
505 g_return_val_if_fail (context != NULL, FALSE);
507 s = gst_context_get_structure (context);
508 ret = gst_structure_get (s, GST_VULKAN_DISPLAY_CONTEXT_TYPE_STR,
509 GST_TYPE_VULKAN_DISPLAY, display, NULL);
511 GST_CAT_LOG (GST_CAT_CONTEXT, "got GstVulkanDisplay(%" GST_PTR_FORMAT
512 ") from context(%" GST_PTR_FORMAT ")", *display, context);
517 typedef gboolean (*InstanceGetExtensionInfo) (GstVulkanInstance * instance,
518 const gchar * name, guint32 * spec_version);
520 static GstVulkanDisplayType
521 gst_vulkan_display_choose_type_full (GstVulkanInstance * instance,
522 InstanceGetExtensionInfo get_ext_info)
524 const gchar *window_str;
525 GstVulkanDisplayType type = GST_VULKAN_DISPLAY_TYPE_NONE;
526 GstVulkanDisplayType first_supported = GST_VULKAN_DISPLAY_TYPE_NONE;
528 window_str = g_getenv ("GST_VULKAN_WINDOW");
530 if (!get_ext_info (instance, VK_KHR_SURFACE_EXTENSION_NAME, NULL))
531 /* Vulkan doesn't have support for surfaces */
532 return GST_VULKAN_DISPLAY_TYPE_NONE;
534 #define CHOOSE_WINSYS(lname,uname) \
536 if (!type && g_strcmp0 (window_str, G_STRINGIFY (lname)) == 0) { \
537 type = G_PASTE(GST_VULKAN_DISPLAY_TYPE_,uname); \
539 if (!first_supported && get_ext_info (instance, \
540 gst_vulkan_display_type_to_extension_string (G_PASTE(GST_VULKAN_DISPLAY_TYPE_,uname)), NULL)) \
541 first_supported = G_PASTE(GST_VULKAN_DISPLAY_TYPE_,uname); \
544 #if GST_VULKAN_HAVE_WINDOW_XCB
545 CHOOSE_WINSYS (xcb, XCB);
547 #if GST_VULKAN_HAVE_WINDOW_WAYLAND
548 CHOOSE_WINSYS (wayland, WAYLAND);
550 #if GST_VULKAN_HAVE_WINDOW_COCOA
551 CHOOSE_WINSYS (cocoa, COCOA);
553 #if GST_VULKAN_HAVE_WINDOW_IOS
554 CHOOSE_WINSYS (ios, IOS);
559 #if GST_VULKAN_HAVE_WINDOW_WIN32
560 /* CHOOSE_WINSYS macro doesn't work with "WIN32" */
561 if (!type && g_strcmp0 (window_str, "win32") == 0) {
562 type = GST_VULKAN_DISPLAY_TYPE_WIN32;
565 if (!first_supported && get_ext_info (instance,
566 gst_vulkan_display_type_to_extension_string
567 (GST_VULKAN_DISPLAY_TYPE_WIN32), NULL))
568 first_supported = GST_VULKAN_DISPLAY_TYPE_WIN32;
571 #if GST_VULKAN_HAVE_WINDOW_ANDROID
572 /* CHOOSE_WINSYS macro doesn't work with "ANDROID" */
573 if (!type && g_strcmp0 (window_str, "android") == 0) {
574 type = GST_VULKAN_DISPLAY_TYPE_ANDROID;
577 if (!first_supported && get_ext_info (instance,
578 gst_vulkan_display_type_to_extension_string
579 (GST_VULKAN_DISPLAY_TYPE_ANDROID), NULL))
580 first_supported = GST_VULKAN_DISPLAY_TYPE_ANDROID;
583 /* if there are no winsys enabled at build time, we get a 'unused but set'
584 * warning. Remove that. */
591 return first_supported;
593 return GST_VULKAN_DISPLAY_TYPE_NONE;
596 G_GNUC_INTERNAL GstVulkanDisplayType
597 gst_vulkan_display_choose_type_unlocked (GstVulkanInstance * instance);
599 G_GNUC_INTERNAL gboolean
600 gst_vulkan_instance_get_extension_info_unlocked (GstVulkanInstance * instance,
601 const gchar * name, guint32 * spec_version);
603 G_GNUC_INTERNAL GstVulkanDisplayType
604 gst_vulkan_display_choose_type_unlocked (GstVulkanInstance * instance)
606 return gst_vulkan_display_choose_type_full (instance,
607 (InstanceGetExtensionInfo)
608 gst_vulkan_instance_get_extension_info_unlocked);
612 * gst_vulkan_display_choose_type:
613 * @instance: a #GstVulkanInstance
615 * This function will read the `GST_VULKAN_WINDOW` environment variable for
616 * a user choice or choose the first supported implementation.
618 * gst_vulkan_instance_fill_info() must have been called prior to this function.
620 * Returns: the default #GstVulkanDisplayType #GstVulkanInstance will choose
626 gst_vulkan_display_choose_type (GstVulkanInstance * instance)
628 return gst_vulkan_display_choose_type_full (instance,
629 (InstanceGetExtensionInfo) gst_vulkan_instance_get_extension_info);
633 * gst_vulkan_display_type_to_extension_string:
634 * @type: a #GstVulkanDisplayType
636 * Returns: the Vulkan extension string required for creating a VkSurfaceKHR
637 * using a window system handle or %NULL
642 gst_vulkan_display_type_to_extension_string (GstVulkanDisplayType type)
644 if (type == GST_VULKAN_DISPLAY_TYPE_NONE)
647 #if GST_VULKAN_HAVE_WINDOW_XCB
648 if (type & GST_VULKAN_DISPLAY_TYPE_XCB)
649 return VK_KHR_XCB_SURFACE_EXTENSION_NAME;
651 #if GST_VULKAN_HAVE_WINDOW_WAYLAND
652 if (type & GST_VULKAN_DISPLAY_TYPE_WAYLAND)
653 return VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
655 #if GST_VULKAN_HAVE_WINDOW_COCOA
656 if (type & GST_VULKAN_DISPLAY_TYPE_COCOA)
657 return VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
659 #if GST_VULKAN_HAVE_WINDOW_IOS
660 if (type & GST_VULKAN_DISPLAY_TYPE_IOS)
661 return VK_MVK_IOS_SURFACE_EXTENSION_NAME;
663 #if GST_VULKAN_HAVE_WINDOW_WIN32
664 if (type & GST_VULKAN_DISPLAY_TYPE_WIN32)
665 return VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
667 #if GST_VULKAN_HAVE_WINDOW_ANDROID
668 if (type & GST_VULKAN_DISPLAY_TYPE_ANDROID)
669 return VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
676 * gst_vulkan_display_handle_context_query:
677 * @element: a #GstElement
678 * @query: a #GstQuery of type #GST_QUERY_CONTEXT
679 * @display: (nullable): the #GstVulkanDisplay
681 * If a #GstVulkanDisplay is requested in @query, sets @device as the reply.
683 * Intended for use with element query handlers to respond to #GST_QUERY_CONTEXT
684 * for a #GstVulkanDisplay.
686 * Returns: whether @query was responded to with @display
691 gst_vulkan_display_handle_context_query (GstElement * element, GstQuery * query,
692 GstVulkanDisplay * display)
694 gboolean res = FALSE;
695 const gchar *context_type;
696 GstContext *context, *old_context;
698 g_return_val_if_fail (element != NULL, FALSE);
699 g_return_val_if_fail (query != NULL, FALSE);
700 g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONTEXT, FALSE);
705 gst_query_parse_context_type (query, &context_type);
707 if (g_strcmp0 (context_type, GST_VULKAN_DISPLAY_CONTEXT_TYPE_STR) == 0) {
708 gst_query_parse_context (query, &old_context);
711 context = gst_context_copy (old_context);
713 context = gst_context_new (GST_VULKAN_DISPLAY_CONTEXT_TYPE_STR, TRUE);
715 gst_context_set_vulkan_display (context, display);
716 gst_query_set_context (query, context);
717 gst_context_unref (context);
719 res = display != NULL;
726 * gst_vulkan_display_run_context_query:
727 * @element: a #GstElement
728 * @display: (inout): a #GstVulkanDisplay
730 * Attempt to retrieve a #GstVulkanDisplay using #GST_QUERY_CONTEXT from the
731 * surrounding elements of @element.
733 * Returns: whether @display contains a valid #GstVulkanDisplay
738 gst_vulkan_display_run_context_query (GstElement * element,
739 GstVulkanDisplay ** display)
741 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
742 g_return_val_if_fail (display != NULL, FALSE);
746 if (*display && GST_IS_VULKAN_DISPLAY (*display))
749 gst_vulkan_global_context_query (element,
750 GST_VULKAN_DISPLAY_CONTEXT_TYPE_STR);
752 GST_DEBUG_OBJECT (element, "found display %p", *display);