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.
23 * @title: GstVulkanWindow
24 * @short_description: window/surface abstraction
25 * @see_also: #GstVulkanDisplay, #GstVulkanSwapper
27 * GstVulkanWindow represents a window that elements can render into. A window can
28 * either be a user visible window (onscreen) or hidden (offscreen).
38 #include "gstvkwindow.h"
40 #if GST_VULKAN_HAVE_WINDOW_XCB
41 #include "xcb/gstvkwindow_xcb.h"
43 #if GST_VULKAN_HAVE_WINDOW_WAYLAND
44 #include "wayland/gstvkwindow_wayland.h"
46 #if GST_VULKAN_HAVE_WINDOW_COCOA
47 #include "cocoa/gstvkwindow_cocoa.h"
49 #if GST_VULKAN_HAVE_WINDOW_IOS
50 #include "ios/gstvkwindow_ios.h"
52 #if GST_VULKAN_HAVE_WINDOW_WIN32
53 #include "win32/gstvkwindow_win32.h"
55 #if GST_VULKAN_HAVE_WINDOW_ANDROID
56 #include "android/gstvkwindow_android.h"
59 #define GST_CAT_DEFAULT gst_vulkan_window_debug
60 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
62 struct _GstVulkanWindowPrivate
68 #define GET_PRIV(window) gst_vulkan_window_get_instance_private (window)
70 #define gst_vulkan_window_parent_class parent_class
71 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GstVulkanWindow, gst_vulkan_window,
74 static void gst_vulkan_window_finalize (GObject * object);
76 typedef struct _GstVulkanDummyWindow
78 GstVulkanWindow parent;
81 } GstVulkanDummyWindow;
83 typedef struct _GstVulkanDummyWindowCass
85 GstVulkanWindowClass parent;
86 } GstVulkanDummyWindowClass;
88 GstVulkanDummyWindow *gst_vulkan_dummy_window_new (void);
107 static guint gst_vulkan_window_signals[LAST_SIGNAL] = { 0 };
110 _accum_logical_and (GSignalInvocationHint * ihint, GValue * return_accu,
111 const GValue * handler_return, gpointer data)
113 gboolean val = g_value_get_boolean (handler_return);
114 gboolean val2 = g_value_get_boolean (return_accu);
116 g_value_set_boolean (return_accu, val && val2);
122 gst_vulkan_window_error_quark (void)
124 return g_quark_from_static_string ("gst-gl-window-error-quark");
128 gst_vulkan_window_default_open (GstVulkanWindow * window, GError ** error)
134 gst_vulkan_window_default_close (GstVulkanWindow * window)
141 static gsize _init = 0;
143 if (g_once_init_enter (&_init)) {
144 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "vulkanwindow", 0,
146 g_once_init_leave (&_init, 1);
151 gst_vulkan_window_set_property (GObject * object, guint prop_id,
152 const GValue * value, GParamSpec * pspec)
154 GstVulkanWindow *window = GST_VULKAN_WINDOW (object);
158 window->display = g_value_dup_object (value);
161 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167 gst_vulkan_window_get_property (GObject * object, guint prop_id,
168 GValue * value, GParamSpec * pspec)
170 GstVulkanWindow *window = GST_VULKAN_WINDOW (object);
174 g_value_set_object (value, window->display);
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183 gst_vulkan_window_init (GstVulkanWindow * window)
188 gst_vulkan_window_class_init (GstVulkanWindowClass * klass)
190 GObjectClass *gobject_class = (GObjectClass *) klass;
192 klass->open = GST_DEBUG_FUNCPTR (gst_vulkan_window_default_open);
193 klass->close = GST_DEBUG_FUNCPTR (gst_vulkan_window_default_close);
195 gst_vulkan_window_signals[SIGNAL_CLOSE] =
196 g_signal_new ("close", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0,
197 (GSignalAccumulator) _accum_logical_and, NULL, NULL, G_TYPE_BOOLEAN, 0);
199 gst_vulkan_window_signals[SIGNAL_DRAW] =
200 g_signal_new ("draw", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0,
201 NULL, NULL, NULL, G_TYPE_NONE, 0);
203 gst_vulkan_window_signals[SIGNAL_RESIZE] =
204 g_signal_new ("resize", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0,
205 NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT);
208 * GstVulkanWindow::mouse-event:
209 * @object: the #GstVulkanWindow
210 * @id: the name of the event
211 * @button: the id of the button
212 * @x: the x coordinate of the mouse event
213 * @y: the y coordinate of the mouse event
215 * Will be emitted when a mouse event is received by the #GstVulkanWindow.
219 gst_vulkan_window_signals[SIGNAL_MOUSE_EVENT] =
220 g_signal_new ("mouse-event", G_TYPE_FROM_CLASS (klass),
221 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 4, G_TYPE_STRING,
222 G_TYPE_INT, G_TYPE_DOUBLE, G_TYPE_DOUBLE);
225 * GstVulkanWindow::key-event:
226 * @object: the #GstVulkanWindow
227 * @id: the name of the event
228 * @key: the id of the key pressed
230 * Will be emitted when a key event is received by the #GstVulkanWindow.
234 gst_vulkan_window_signals[SIGNAL_KEY_EVENT] =
235 g_signal_new ("key-event", G_TYPE_FROM_CLASS (klass),
236 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_STRING,
239 gobject_class->set_property = gst_vulkan_window_set_property;
240 gobject_class->get_property = gst_vulkan_window_get_property;
241 gobject_class->finalize = gst_vulkan_window_finalize;
243 g_object_class_install_property (gobject_class, PROP_DISPLAY,
244 g_param_spec_object ("display", "Display",
245 "Associated Vulkan Display",
246 GST_TYPE_VULKAN_DISPLAY, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
252 * gst_vulkan_window_new:
253 * @display: a #GstVulkanDisplay
255 * Returns: (transfer full): a new #GstVulkanWindow using @display's connection
260 gst_vulkan_window_new (GstVulkanDisplay * display)
262 GstVulkanWindow *window = NULL;
263 const gchar *user_choice;
265 g_return_val_if_fail (display != NULL, NULL);
269 user_choice = g_getenv ("GST_VULKAN_WINDOW");
270 GST_INFO ("creating a window, user choice:%s", user_choice);
271 #if GST_VULKAN_HAVE_WINDOW_XCB
272 if (!window && (!user_choice || g_strstr_len (user_choice, 3, "xcb")))
273 window = GST_VULKAN_WINDOW (gst_vulkan_window_xcb_new (display));
275 #if GST_VULKAN_HAVE_WINDOW_WAYLAND
276 if (!window && (!user_choice || g_strstr_len (user_choice, 7, "wayland")))
277 window = GST_VULKAN_WINDOW (gst_vulkan_window_wayland_new (display));
279 #if GST_VULKAN_HAVE_WINDOW_COCOA
280 if (!window && (!user_choice || g_strstr_len (user_choice, 5, "cocoa")))
281 window = GST_VULKAN_WINDOW (gst_vulkan_window_cocoa_new (display));
283 #if GST_VULKAN_HAVE_WINDOW_IOS
284 if (!window && (!user_choice || g_strstr_len (user_choice, 3, "ios")))
285 window = GST_VULKAN_WINDOW (gst_vulkan_window_ios_new (display));
287 #if GST_VULKAN_HAVE_WINDOW_WIN32
288 if (!window && (!user_choice || g_strstr_len (user_choice, 5, "win32")))
289 window = GST_VULKAN_WINDOW (gst_vulkan_window_win32_new (display));
291 #if GST_VULKAN_HAVE_WINDOW_ANDROID
292 if (!window && (!user_choice || g_strstr_len (user_choice, 7, "android")))
293 window = GST_VULKAN_WINDOW (gst_vulkan_window_android_new (display));
296 /* subclass returned a NULL window */
297 GST_WARNING ("Could not create window. user specified %s, creating dummy"
298 " window", user_choice ? user_choice : "(null)");
300 window = GST_VULKAN_WINDOW (gst_vulkan_dummy_window_new ());
303 window->display = gst_object_ref (display);
309 gst_vulkan_window_finalize (GObject * object)
311 GstVulkanWindow *window = GST_VULKAN_WINDOW (object);
313 gst_object_unref (window->display);
315 G_OBJECT_CLASS (gst_vulkan_window_parent_class)->finalize (object);
319 * gst_vulkan_window_get_display:
320 * @window: a #GstVulkanWindow
322 * Returns: (transfer full): the #GstVulkanDisplay for @window
327 gst_vulkan_window_get_display (GstVulkanWindow * window)
329 g_return_val_if_fail (GST_IS_VULKAN_WINDOW (window), NULL);
331 return gst_object_ref (window->display);
335 * gst_vulkan_window_get_surface: (skip)
336 * @window: a #GstVulkanWindow
339 * Returns: the VkSurface for displaying into. The caller is responsible for
340 * calling `VkDestroySurface` on the returned surface.
345 gst_vulkan_window_get_surface (GstVulkanWindow * window, GError ** error)
347 GstVulkanWindowClass *klass;
349 g_return_val_if_fail (GST_IS_VULKAN_WINDOW (window), (VkSurfaceKHR) 0);
350 klass = GST_VULKAN_WINDOW_GET_CLASS (window);
351 g_return_val_if_fail (klass->get_surface != NULL, (VkSurfaceKHR) 0);
353 return klass->get_surface (window, error);
357 * gst_vulkan_window_get_presentation_support:
358 * @window: a #GstVulkanWindow
359 * @device: a #GstVulkanDevice
360 * @queue_family_idx: the queue family
362 * Returns: whether the given combination of @window, @device and
363 * @queue_family_idx supports presentation
368 gst_vulkan_window_get_presentation_support (GstVulkanWindow * window,
369 GstVulkanDevice * device, guint32 queue_family_idx)
371 GstVulkanWindowClass *klass;
373 g_return_val_if_fail (GST_IS_VULKAN_WINDOW (window), FALSE);
374 klass = GST_VULKAN_WINDOW_GET_CLASS (window);
375 g_return_val_if_fail (klass->get_presentation_support != NULL, FALSE);
377 return klass->get_presentation_support (window, device, queue_family_idx);
381 * gst_vulkan_window_open:
382 * @window: a #GstVulkanWindow
385 * Returns: whether @window could be successfully opened
390 gst_vulkan_window_open (GstVulkanWindow * window, GError ** error)
392 GstVulkanWindowClass *klass;
394 g_return_val_if_fail (GST_IS_VULKAN_WINDOW (window), FALSE);
395 klass = GST_VULKAN_WINDOW_GET_CLASS (window);
396 g_return_val_if_fail (klass->open != NULL, FALSE);
398 return klass->open (window, error);
402 * gst_vulkan_window_close:
403 * @window: a #GstVulkanWindow
405 * Attempt to close the window.
410 gst_vulkan_window_close (GstVulkanWindow * window)
412 GstVulkanWindowClass *klass;
415 g_return_if_fail (GST_IS_VULKAN_WINDOW (window));
416 klass = GST_VULKAN_WINDOW_GET_CLASS (window);
417 g_return_if_fail (klass->close != NULL);
419 g_signal_emit (window, gst_vulkan_window_signals[SIGNAL_CLOSE], 0, &to_close);
422 klass->close (window);
426 * gst_vulkan_window_resize:
427 * @window: a #GstVulkanWindow
428 * @width: the new width
429 * @height: the new height
431 * Resize the output surface.
433 * Currently intended for subclasses to update internal state.
438 gst_vulkan_window_resize (GstVulkanWindow * window, gint width, gint height)
440 GstVulkanWindowPrivate *priv;
442 g_return_if_fail (GST_IS_VULKAN_WINDOW (window));
444 priv = GET_PRIV (window);
446 priv->surface_width = width;
447 priv->surface_height = height;
449 g_signal_emit (window, gst_vulkan_window_signals[SIGNAL_RESIZE], 0, width,
454 * gst_vulkan_window_redraw:
455 * @window: a #GstVulkanWindow
457 * Ask the @window to redraw its contents
462 gst_vulkan_window_redraw (GstVulkanWindow * window)
464 g_return_if_fail (GST_IS_VULKAN_WINDOW (window));
466 g_signal_emit (window, gst_vulkan_window_signals[SIGNAL_DRAW], 0);
470 gst_vulkan_window_set_window_handle (GstVulkanWindow * window, guintptr handle)
472 GstVulkanWindowClass *klass;
474 g_return_if_fail (GST_IS_VULKAN_WINDOW (window));
475 klass = GST_VULKAN_WINDOW_GET_CLASS (window);
477 if (!klass->set_window_handle) {
479 g_warning ("%s does not implement the set_window_handle vfunc. "
480 "Output will not be embedded into the specified surface.",
481 GST_OBJECT_NAME (window));
483 klass->set_window_handle (window, handle);
488 gst_vulkan_window_get_surface_dimensions (GstVulkanWindow * window,
489 guint * width, guint * height)
491 GstVulkanWindowPrivate *priv;
492 GstVulkanWindowClass *klass;
494 g_return_if_fail (GST_IS_VULKAN_WINDOW (window));
495 klass = GST_VULKAN_WINDOW_GET_CLASS (window);
496 priv = GET_PRIV (window);
498 if (klass->get_surface_dimensions) {
499 klass->get_surface_dimensions (window, width, height);
501 GST_DEBUG_OBJECT (window, "Returning size %ix%i",
502 priv->surface_width, priv->surface_height);
503 *width = priv->surface_width;
504 *height = priv->surface_height;
509 gst_vulkan_window_send_key_event (GstVulkanWindow * window,
510 const char *event_type, const char *key_str)
512 g_return_if_fail (GST_IS_VULKAN_WINDOW (window));
514 g_signal_emit (window, gst_vulkan_window_signals[SIGNAL_KEY_EVENT], 0,
515 event_type, key_str);
519 gst_vulkan_window_send_mouse_event (GstVulkanWindow * window,
520 const char *event_type, int button, double posx, double posy)
522 g_return_if_fail (GST_IS_VULKAN_WINDOW (window));
524 g_signal_emit (window, gst_vulkan_window_signals[SIGNAL_MOUSE_EVENT], 0,
525 event_type, button, posx, posy);
529 * gst_vulkan_window_handle_events:
530 * @window: a #GstVulkanWindow
531 * @handle_events: a #gboolean indicating if events should be handled or not.
533 * Tell a @window that it should handle events from the window system. These
534 * events are forwarded upstream as navigation events. In some window systems
535 * events are not propagated in the window hierarchy if a client is listening
536 * for them. This method allows you to disable events handling completely
542 gst_vulkan_window_handle_events (GstVulkanWindow * window,
543 gboolean handle_events)
545 GstVulkanWindowClass *window_class;
547 g_return_if_fail (GST_IS_VULKAN_WINDOW (window));
548 window_class = GST_VULKAN_WINDOW_GET_CLASS (window);
550 if (window_class->handle_events)
551 window_class->handle_events (window, handle_events);
554 GType gst_vulkan_dummy_window_get_type (void);
555 G_DEFINE_TYPE (GstVulkanDummyWindow, gst_vulkan_dummy_window,
556 GST_TYPE_VULKAN_WINDOW);
559 gst_vulkan_dummy_window_class_init (GstVulkanDummyWindowClass * klass)
564 gst_vulkan_dummy_window_init (GstVulkanDummyWindow * dummy)
569 GstVulkanDummyWindow *
570 gst_vulkan_dummy_window_new (void)
572 GstVulkanDummyWindow *window;
574 window = g_object_new (gst_vulkan_dummy_window_get_type (), NULL);
575 gst_object_ref_sink (window);