1 /*
2 * GStreamer
3 * Copyright (C) 2015 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 "gstvkdisplay.h"
26
27 #if GST_VULKAN_HAVE_WINDOW_XCB
28 #include "xcb/gstvkdisplay_xcb.h"
29 #endif
30 #if GST_VULKAN_HAVE_WINDOW_WAYLAND
31 #include "wayland/gstvkdisplay_wayland.h"
32 #endif
33 #if GST_VULKAN_HAVE_WINDOW_COCOA
34 #include "cocoa/gstvkdisplay_cocoa.h"
35 #endif
36 #if GST_VULKAN_HAVE_WINDOW_IOS
37 #include "ios/gstvkdisplay_ios.h"
38 #endif
39 #if GST_VULKAN_HAVE_WINDOW_WIN32
40 #include "win32/gstvkwindow_win32.h"
41 #endif
42 #if GST_VULKAN_HAVE_WINDOW_ANDROID
43 #include "android/gstvkdisplay_android.h"
44 #endif
45
46 /**
47 * SECTION:vkdisplay
48 * @title: GstVulkanDisplay
49 * @short_description: window system display
50 * @see_also: #GstVulkanInstance, #GstVulkanWindow
51 *
52 * A #GstVulkanDisplay represents a connection to a display server on the platform
53 */
54
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);
58
59 static void
_init_debug(void)60 _init_debug (void)
61 {
62 static gsize _init = 0;
63
64 if (g_once_init_enter (&_init)) {
65 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "vulkandisplay", 0,
66 "Vulkan display");
67 GST_DEBUG_CATEGORY_GET (GST_CAT_CONTEXT, "GST_CONTEXT");
68 g_once_init_leave (&_init, 1);
69 }
70 }
71
72 enum
73 {
74 SIGNAL_0,
75 CREATE_CONTEXT,
76 LAST_SIGNAL
77 };
78
79 /* static guint gst_vulkan_display_signals[LAST_SIGNAL] = { 0 }; */
80
81 static void gst_vulkan_display_finalize (GObject * object);
82 static gpointer gst_vulkan_display_default_get_handle (GstVulkanDisplay *
83 display);
84 static GstVulkanWindow
85 * gst_vulkan_display_default_create_window (GstVulkanDisplay * display);
86
87 struct _GstVulkanDisplayPrivate
88 {
89 GThread *event_thread;
90
91 GMutex thread_lock;
92 GCond thread_cond;
93 };
94
95 #define GET_PRIV(display) gst_vulkan_display_get_instance_private (display)
96
97 G_DEFINE_TYPE_WITH_CODE (GstVulkanDisplay, gst_vulkan_display, GST_TYPE_OBJECT,
98 G_ADD_PRIVATE (GstVulkanDisplay) _init_debug ());
99
100 static gpointer
_event_thread_main(GstVulkanDisplay * display)101 _event_thread_main (GstVulkanDisplay * display)
102 {
103 GstVulkanDisplayPrivate *priv = GET_PRIV (display);
104
105 g_mutex_lock (&priv->thread_lock);
106
107 display->main_context = g_main_context_new ();
108 display->main_loop = g_main_loop_new (display->main_context, FALSE);
109
110 g_cond_broadcast (&priv->thread_cond);
111 g_mutex_unlock (&priv->thread_lock);
112
113 g_main_loop_run (display->main_loop);
114
115 g_mutex_lock (&priv->thread_lock);
116
117 g_main_loop_unref (display->main_loop);
118 g_main_context_unref (display->main_context);
119
120 display->main_loop = NULL;
121 display->main_context = NULL;
122
123 g_cond_broadcast (&priv->thread_cond);
124 g_mutex_unlock (&priv->thread_lock);
125
126 return NULL;
127 }
128
129 static void
gst_vulkan_display_class_init(GstVulkanDisplayClass * klass)130 gst_vulkan_display_class_init (GstVulkanDisplayClass * klass)
131 {
132 klass->get_handle = gst_vulkan_display_default_get_handle;
133 klass->create_window = gst_vulkan_display_default_create_window;
134
135 G_OBJECT_CLASS (klass)->finalize = gst_vulkan_display_finalize;
136 }
137
138 static void
gst_vulkan_display_init(GstVulkanDisplay * display)139 gst_vulkan_display_init (GstVulkanDisplay * display)
140 {
141 GstVulkanDisplayPrivate *priv = GET_PRIV (display);
142
143 display->type = GST_VULKAN_DISPLAY_TYPE_ANY;
144
145 g_mutex_init (&priv->thread_lock);
146 g_cond_init (&priv->thread_cond);
147
148 priv->event_thread = g_thread_new ("vkdisplay-event",
149 (GThreadFunc) _event_thread_main, display);
150
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);
155 }
156
157 static void
free_window_weak_ref(GWeakRef * ref)158 free_window_weak_ref (GWeakRef * ref)
159 {
160 if (!ref)
161 return;
162
163 g_weak_ref_clear (ref);
164 g_free (ref);
165 }
166
167 static void
gst_vulkan_display_finalize(GObject * object)168 gst_vulkan_display_finalize (GObject * object)
169 {
170 GstVulkanDisplay *display = GST_VULKAN_DISPLAY (object);
171 GstVulkanDisplayPrivate *priv = GET_PRIV (display);
172
173 g_mutex_lock (&priv->thread_lock);
174
175 if (display->main_loop)
176 g_main_loop_quit (display->main_loop);
177
178 while (display->main_loop)
179 g_cond_wait (&priv->thread_cond, &priv->thread_lock);
180
181 if (priv->event_thread)
182 g_thread_unref (priv->event_thread);
183 priv->event_thread = NULL;
184 g_mutex_unlock (&priv->thread_lock);
185
186 if (display->event_source) {
187 g_source_destroy (display->event_source);
188 g_source_unref (display->event_source);
189 }
190 display->event_source = NULL;
191
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);
196
197 if (display->instance) {
198 gst_object_unref (display->instance);
199 }
200
201 G_OBJECT_CLASS (gst_vulkan_display_parent_class)->finalize (object);
202 }
203
204 /**
205 * gst_vulkan_display_new_with_type:
206 * @instance: a #GstVulkanInstance
207 * @type: the #GstVulkanDisplayType to create
208 *
209 * Returns: (transfer full): a new #GstVulkanDisplay or %NULL if e.g. @type is
210 * unsupported
211 *
212 * Since: 1.18
213 */
214 GstVulkanDisplay *
gst_vulkan_display_new_with_type(GstVulkanInstance * instance,GstVulkanDisplayType type)215 gst_vulkan_display_new_with_type (GstVulkanInstance * instance,
216 GstVulkanDisplayType type)
217 {
218 GstVulkanDisplay *display = NULL;
219
220 _init_debug ();
221
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));
225 }
226 #endif
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));
230 }
231 #endif
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 ());
235 }
236 #endif
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 ());
240 }
241 #endif
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 ());
245 }
246 #endif
247
248 if (display)
249 display->instance = gst_object_ref (instance);
250
251 return display;
252 }
253
254 /**
255 * gst_vulkan_display_new:
256 *
257 * Returns: (transfer full): a new #GstVulkanDisplay
258 *
259 * Since: 1.18
260 */
261 GstVulkanDisplay *
gst_vulkan_display_new(GstVulkanInstance * instance)262 gst_vulkan_display_new (GstVulkanInstance * instance)
263 {
264 GstVulkanDisplayType type;
265 GstVulkanDisplay *display = NULL;
266
267 type = gst_vulkan_display_choose_type (instance);
268 display = gst_vulkan_display_new_with_type (instance, type);
269
270 if (!display) {
271 /* subclass returned a NULL display */
272 GST_FIXME ("creating dummy display");
273
274 display = g_object_new (GST_TYPE_VULKAN_DISPLAY, NULL);
275 gst_object_ref_sink (display);
276 display->instance = gst_object_ref (instance);
277 }
278
279 return display;
280 }
281
282 /**
283 * gst_vulkan_display_get_handle:
284 * @display: a #GstVulkanDisplay
285 *
286 * Returns: the winsys specific handle of @display
287 *
288 * Since: 1.18
289 */
290 gpointer
gst_vulkan_display_get_handle(GstVulkanDisplay * display)291 gst_vulkan_display_get_handle (GstVulkanDisplay * display)
292 {
293 GstVulkanDisplayClass *klass;
294
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);
298
299 return klass->get_handle (display);
300 }
301
302 static gpointer
gst_vulkan_display_default_get_handle(GstVulkanDisplay * display)303 gst_vulkan_display_default_get_handle (GstVulkanDisplay * display)
304 {
305 return 0;
306 }
307
308 /**
309 * gst_vulkan_display_get_handle_type:
310 * @display: a #GstVulkanDisplay
311 *
312 * Returns: the #GstVulkanDisplayType of @display
313 *
314 * Since: 1.18
315 */
316 GstVulkanDisplayType
gst_vulkan_display_get_handle_type(GstVulkanDisplay * display)317 gst_vulkan_display_get_handle_type (GstVulkanDisplay * display)
318 {
319 g_return_val_if_fail (GST_IS_VULKAN_DISPLAY (display),
320 GST_VULKAN_DISPLAY_TYPE_NONE);
321
322 return display->type;
323 }
324
325 /**
326 * gst_vulkan_display_create_window:
327 * @display: a #GstVulkanDisplay
328 *
329 * Returns: (transfer full): a new #GstVulkanWindow for @display or %NULL.
330 *
331 * Since: 1.18
332 */
333 GstVulkanWindow *
gst_vulkan_display_create_window(GstVulkanDisplay * display)334 gst_vulkan_display_create_window (GstVulkanDisplay * display)
335 {
336 GstVulkanDisplayClass *klass;
337 GstVulkanWindow *window;
338
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);
342
343 window = klass->create_window (display);
344
345 if (window) {
346 GWeakRef *ref = g_new0 (GWeakRef, 1);
347
348 g_weak_ref_set (ref, window);
349
350 GST_OBJECT_LOCK (display);
351 display->windows = g_list_prepend (display->windows, ref);
352 GST_OBJECT_UNLOCK (display);
353 }
354
355 return window;
356 }
357
358 static GstVulkanWindow *
gst_vulkan_display_default_create_window(GstVulkanDisplay * display)359 gst_vulkan_display_default_create_window (GstVulkanDisplay * display)
360 {
361 return gst_vulkan_window_new (display);
362 }
363
364 static gint
_compare_vulkan_window(GWeakRef * ref,GstVulkanWindow * window)365 _compare_vulkan_window (GWeakRef * ref, GstVulkanWindow * window)
366 {
367 GstVulkanWindow *other = g_weak_ref_get (ref);
368 gboolean equal = window == other;
369
370 if (other)
371 gst_object_unref (other);
372
373 return !equal;
374 }
375
376 static void
prepend_window_weakref_item(GWeakRef * ref,GList ** new)377 prepend_window_weakref_item (GWeakRef * ref, GList ** new)
378 {
379 GstVulkanWindow *window = g_weak_ref_get (ref);
380 if (window) {
381 *new = g_list_prepend (*new, window);
382 }
383 }
384
385 static GList *
window_weak_list_to_strong(GstVulkanDisplay * display)386 window_weak_list_to_strong (GstVulkanDisplay * display)
387 {
388 GList *new = NULL;
389 g_list_foreach (display->windows, (GFunc) prepend_window_weakref_item, &new);
390 return new;
391 }
392
393 /**
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
398 *
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.
402 *
403 * Returns: (transfer full): The first #GstVulkanWindow that causes a match
404 * from @compare_func
405 *
406 * Since: 1.18
407 */
408 GstVulkanWindow *
gst_vulkan_display_find_window(GstVulkanDisplay * display,gpointer data,GCompareFunc compare_func)409 gst_vulkan_display_find_window (GstVulkanDisplay * display, gpointer data,
410 GCompareFunc compare_func)
411 {
412 GstVulkanWindow *ret = NULL;
413 GList *l, *windows;
414
415 GST_OBJECT_LOCK (display);
416 windows = window_weak_list_to_strong (display);
417 l = g_list_find_custom (windows, data, (GCompareFunc) compare_func);
418 if (l)
419 ret = gst_object_ref (l->data);
420
421 GST_DEBUG_OBJECT (display, "Found window %" GST_PTR_FORMAT
422 " (%p) in internal list", ret, ret);
423 GST_OBJECT_UNLOCK (display);
424
425 g_list_free_full (windows, gst_object_unref);
426
427 return ret;
428 }
429
430 /**
431 * gst_vulkan_display_remove_window:
432 * @display: a #GstVulkanDisplay
433 * @window: the #GstVulkanWindow to remove
434 *
435 * Returns: whether the window was successfully removed
436 *
437 * Since: 1.18
438 */
439 gboolean
gst_vulkan_display_remove_window(GstVulkanDisplay * display,GstVulkanWindow * window)440 gst_vulkan_display_remove_window (GstVulkanDisplay * display,
441 GstVulkanWindow * window)
442 {
443 gboolean ret = FALSE;
444 GList *l;
445
446 GST_OBJECT_LOCK (display);
447 l = g_list_find_custom (display->windows, window,
448 (GCompareFunc) _compare_vulkan_window);
449 if (l) {
450 GWeakRef *ref = l->data;
451 display->windows = g_list_delete_link (display->windows, l);
452 free_window_weak_ref (ref);
453 ret = TRUE;
454 }
455 GST_OBJECT_UNLOCK (display);
456
457 return ret;
458 }
459
460 /**
461 * gst_context_set_vulkan_display:
462 * @context: a #GstContext
463 * @display: a #GstVulkanDisplay
464 *
465 * Sets @display on @context
466 *
467 * Since: 1.18
468 */
469 void
gst_context_set_vulkan_display(GstContext * context,GstVulkanDisplay * display)470 gst_context_set_vulkan_display (GstContext * context,
471 GstVulkanDisplay * display)
472 {
473 GstStructure *s;
474
475 g_return_if_fail (context != NULL);
476 g_return_if_fail (gst_context_is_writable (context));
477
478 if (display)
479 GST_CAT_LOG (GST_CAT_CONTEXT,
480 "setting GstVulkanDisplay(%" GST_PTR_FORMAT ") on context(%"
481 GST_PTR_FORMAT ")", display, context);
482
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);
486 }
487
488 /**
489 * gst_context_get_vulkan_display:
490 * @context: a #GstContext
491 * @display: resulting #GstVulkanDisplay
492 *
493 * Returns: Whether @display was in @context
494 *
495 * Since: 1.18
496 */
497 gboolean
gst_context_get_vulkan_display(GstContext * context,GstVulkanDisplay ** display)498 gst_context_get_vulkan_display (GstContext * context,
499 GstVulkanDisplay ** display)
500 {
501 const GstStructure *s;
502 gboolean ret;
503
504 g_return_val_if_fail (display != NULL, FALSE);
505 g_return_val_if_fail (context != NULL, FALSE);
506
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);
510
511 GST_CAT_LOG (GST_CAT_CONTEXT, "got GstVulkanDisplay(%" GST_PTR_FORMAT
512 ") from context(%" GST_PTR_FORMAT ")", *display, context);
513
514 return ret;
515 }
516
517 typedef gboolean (*InstanceGetExtensionInfo) (GstVulkanInstance * instance,
518 const gchar * name, guint32 * spec_version);
519
520 static GstVulkanDisplayType
gst_vulkan_display_choose_type_full(GstVulkanInstance * instance,InstanceGetExtensionInfo get_ext_info)521 gst_vulkan_display_choose_type_full (GstVulkanInstance * instance,
522 InstanceGetExtensionInfo get_ext_info)
523 {
524 const gchar *window_str;
525 GstVulkanDisplayType type = GST_VULKAN_DISPLAY_TYPE_NONE;
526 GstVulkanDisplayType first_supported = GST_VULKAN_DISPLAY_TYPE_NONE;
527
528 window_str = g_getenv ("GST_VULKAN_WINDOW");
529
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;
533
534 #define CHOOSE_WINSYS(lname,uname) \
535 G_STMT_START { \
536 if (!type && g_strcmp0 (window_str, G_STRINGIFY (lname)) == 0) { \
537 type = G_PASTE(GST_VULKAN_DISPLAY_TYPE_,uname); \
538 } \
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); \
542 } G_STMT_END
543
544 #if GST_VULKAN_HAVE_WINDOW_XCB
545 CHOOSE_WINSYS (xcb, XCB);
546 #endif
547 #if GST_VULKAN_HAVE_WINDOW_WAYLAND
548 CHOOSE_WINSYS (wayland, WAYLAND);
549 #endif
550 #if GST_VULKAN_HAVE_WINDOW_COCOA
551 CHOOSE_WINSYS (cocoa, COCOA);
552 #endif
553 #if GST_VULKAN_HAVE_WINDOW_IOS
554 CHOOSE_WINSYS (ios, IOS);
555 #endif
556
557 #undef CHOOSE_WINSYS
558
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;
563 }
564
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;
569 #endif
570
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;
575 }
576
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;
581 #endif
582
583 /* if there are no winsys enabled at build time, we get a 'unused but set'
584 * warning. Remove that. */
585 (void) window_str;
586
587 if (type)
588 return type;
589
590 if (first_supported)
591 return first_supported;
592
593 return GST_VULKAN_DISPLAY_TYPE_NONE;
594 }
595
596 G_GNUC_INTERNAL GstVulkanDisplayType
597 gst_vulkan_display_choose_type_unlocked (GstVulkanInstance * instance);
598
599 G_GNUC_INTERNAL gboolean
600 gst_vulkan_instance_get_extension_info_unlocked (GstVulkanInstance * instance,
601 const gchar * name, guint32 * spec_version);
602
603 G_GNUC_INTERNAL GstVulkanDisplayType
gst_vulkan_display_choose_type_unlocked(GstVulkanInstance * instance)604 gst_vulkan_display_choose_type_unlocked (GstVulkanInstance * instance)
605 {
606 return gst_vulkan_display_choose_type_full (instance,
607 (InstanceGetExtensionInfo)
608 gst_vulkan_instance_get_extension_info_unlocked);
609 }
610
611 /**
612 * gst_vulkan_display_choose_type:
613 * @instance: a #GstVulkanInstance
614 *
615 * This function will read the `GST_VULKAN_WINDOW` environment variable for
616 * a user choice or choose the first supported implementation.
617 *
618 * gst_vulkan_instance_fill_info() must have been called prior to this function.
619 *
620 * Returns: the default #GstVulkanDisplayType #GstVulkanInstance will choose
621 * on creation
622 *
623 * Since: 1.18
624 */
625 GstVulkanDisplayType
gst_vulkan_display_choose_type(GstVulkanInstance * instance)626 gst_vulkan_display_choose_type (GstVulkanInstance * instance)
627 {
628 return gst_vulkan_display_choose_type_full (instance,
629 (InstanceGetExtensionInfo) gst_vulkan_instance_get_extension_info);
630 }
631
632 /**
633 * gst_vulkan_display_type_to_extension_string:
634 * @type: a #GstVulkanDisplayType
635 *
636 * Returns: the Vulkan extension string required for creating a VkSurfaceKHR
637 * using a window system handle or %NULL
638 *
639 * Since: 1.18
640 */
641 const gchar *
gst_vulkan_display_type_to_extension_string(GstVulkanDisplayType type)642 gst_vulkan_display_type_to_extension_string (GstVulkanDisplayType type)
643 {
644 if (type == GST_VULKAN_DISPLAY_TYPE_NONE)
645 return NULL;
646
647 #if GST_VULKAN_HAVE_WINDOW_XCB
648 if (type & GST_VULKAN_DISPLAY_TYPE_XCB)
649 return VK_KHR_XCB_SURFACE_EXTENSION_NAME;
650 #endif
651 #if GST_VULKAN_HAVE_WINDOW_WAYLAND
652 if (type & GST_VULKAN_DISPLAY_TYPE_WAYLAND)
653 return VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
654 #endif
655 #if GST_VULKAN_HAVE_WINDOW_COCOA
656 if (type & GST_VULKAN_DISPLAY_TYPE_COCOA)
657 return VK_MVK_MACOS_SURFACE_EXTENSION_NAME;
658 #endif
659 #if GST_VULKAN_HAVE_WINDOW_IOS
660 if (type & GST_VULKAN_DISPLAY_TYPE_IOS)
661 return VK_MVK_IOS_SURFACE_EXTENSION_NAME;
662 #endif
663 #if GST_VULKAN_HAVE_WINDOW_WIN32
664 if (type & GST_VULKAN_DISPLAY_TYPE_WIN32)
665 return VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
666 #endif
667 #if GST_VULKAN_HAVE_WINDOW_ANDROID
668 if (type & GST_VULKAN_DISPLAY_TYPE_ANDROID)
669 return VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
670 #endif
671
672 return NULL;
673 }
674
675 /**
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
680 *
681 * If a #GstVulkanDisplay is requested in @query, sets @device as the reply.
682 *
683 * Intended for use with element query handlers to respond to #GST_QUERY_CONTEXT
684 * for a #GstVulkanDisplay.
685 *
686 * Returns: whether @query was responded to with @display
687 *
688 * Since: 1.18
689 */
690 gboolean
gst_vulkan_display_handle_context_query(GstElement * element,GstQuery * query,GstVulkanDisplay * display)691 gst_vulkan_display_handle_context_query (GstElement * element, GstQuery * query,
692 GstVulkanDisplay * display)
693 {
694 gboolean res = FALSE;
695 const gchar *context_type;
696 GstContext *context, *old_context;
697
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);
701
702 if (!display)
703 return FALSE;
704
705 gst_query_parse_context_type (query, &context_type);
706
707 if (g_strcmp0 (context_type, GST_VULKAN_DISPLAY_CONTEXT_TYPE_STR) == 0) {
708 gst_query_parse_context (query, &old_context);
709
710 if (old_context)
711 context = gst_context_copy (old_context);
712 else
713 context = gst_context_new (GST_VULKAN_DISPLAY_CONTEXT_TYPE_STR, TRUE);
714
715 gst_context_set_vulkan_display (context, display);
716 gst_query_set_context (query, context);
717 gst_context_unref (context);
718
719 res = display != NULL;
720 }
721
722 return res;
723 }
724
725 /**
726 * gst_vulkan_display_run_context_query:
727 * @element: a #GstElement
728 * @display: (inout): a #GstVulkanDisplay
729 *
730 * Attempt to retrieve a #GstVulkanDisplay using #GST_QUERY_CONTEXT from the
731 * surrounding elements of @element.
732 *
733 * Returns: whether @display contains a valid #GstVulkanDisplay
734 *
735 * Since: 1.18
736 */
737 gboolean
gst_vulkan_display_run_context_query(GstElement * element,GstVulkanDisplay ** display)738 gst_vulkan_display_run_context_query (GstElement * element,
739 GstVulkanDisplay ** display)
740 {
741 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
742 g_return_val_if_fail (display != NULL, FALSE);
743
744 _init_debug ();
745
746 if (*display && GST_IS_VULKAN_DISPLAY (*display))
747 return TRUE;
748
749 gst_vulkan_global_context_query (element,
750 GST_VULKAN_DISPLAY_CONTEXT_TYPE_STR);
751
752 GST_DEBUG_OBJECT (element, "found display %p", *display);
753
754 if (*display)
755 return TRUE;
756
757 return FALSE;
758 }
759