vulkan: Add iOS window implementation
[platform/upstream/gst-plugins-bad.git] / ext / vulkan / vkwindow.c
1 /*
2  * GStreamer
3  * Copyright (C) 2012 Matthew Waters <ystreet00@gmail.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 /**
22  * SECTION:vkwindow
23  * @short_description: window/surface abstraction
24  * @title: GstVulkanWindow
25  * @see_also: #GstGLContext, #GstGLDisplay
26  *
27  * GstVulkanWindow represents a window that elements can render into.  A window can
28  * either be a user visible window (onscreen) or hidden (offscreen).
29  */
30
31 #if HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <gmodule.h>
36 #include <stdio.h>
37
38 #include "vkwindow.h"
39
40 #if GST_VULKAN_HAVE_WINDOW_X11
41 #include "x11/vkwindow_x11.h"
42 #endif
43 #if GST_VULKAN_HAVE_WINDOW_XCB
44 #include "xcb/vkwindow_xcb.h"
45 #endif
46 #if GST_VULKAN_HAVE_WINDOW_WAYLAND
47 #include "wayland/vkwindow_wayland.h"
48 #endif
49 #if GST_VULKAN_HAVE_WINDOW_COCOA
50 #include "cocoa/vkwindow_cocoa.h"
51 #endif
52 #if GST_VULKAN_HAVE_WINDOW_IOS
53 #include "ios/vkwindow_ios.h"
54 #endif
55
56 #define GST_CAT_DEFAULT gst_vulkan_window_debug
57 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
58
59 struct _GstVulkanWindowPrivate
60 {
61   guint surface_width;
62   guint surface_height;
63 };
64
65 #define gst_vulkan_window_parent_class parent_class
66 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GstVulkanWindow, gst_vulkan_window,
67     GST_TYPE_OBJECT);
68
69 static void gst_vulkan_window_finalize (GObject * object);
70
71 typedef struct _GstVulkanDummyWindow
72 {
73   GstVulkanWindow parent;
74
75   guintptr handle;
76 } GstVulkanDummyWindow;
77
78 typedef struct _GstVulkanDummyWindowCass
79 {
80   GstVulkanWindowClass parent;
81 } GstVulkanDummyWindowClass;
82
83 GstVulkanDummyWindow *gst_vulkan_dummy_window_new (void);
84
85 enum
86 {
87   SIGNAL_0,
88   SIGNAL_CLOSE,
89   SIGNAL_DRAW,
90   LAST_SIGNAL
91 };
92
93 static guint gst_vulkan_window_signals[LAST_SIGNAL] = { 0 };
94
95 static gboolean
96 _accum_logical_and (GSignalInvocationHint * ihint, GValue * return_accu,
97     const GValue * handler_return, gpointer data)
98 {
99   gboolean val = g_value_get_boolean (handler_return);
100   gboolean val2 = g_value_get_boolean (return_accu);
101
102   g_value_set_boolean (return_accu, val && val2);
103
104   return TRUE;
105 }
106
107 GQuark
108 gst_vulkan_window_error_quark (void)
109 {
110   return g_quark_from_static_string ("gst-gl-window-error-quark");
111 }
112
113 static gboolean
114 gst_vulkan_window_default_open (GstVulkanWindow * window, GError ** error)
115 {
116   return TRUE;
117 }
118
119 static void
120 gst_vulkan_window_default_close (GstVulkanWindow * window)
121 {
122 }
123
124 static void
125 _init_debug (void)
126 {
127   static volatile gsize _init = 0;
128
129   if (g_once_init_enter (&_init)) {
130     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "vulkanwindow", 0,
131         "Vulkan Window");
132     g_once_init_leave (&_init, 1);
133   }
134 }
135
136 static void
137 gst_vulkan_window_init (GstVulkanWindow * window)
138 {
139   window->priv = gst_vulkan_window_get_instance_private (window);
140 }
141
142 static void
143 gst_vulkan_window_class_init (GstVulkanWindowClass * klass)
144 {
145   klass->open = GST_DEBUG_FUNCPTR (gst_vulkan_window_default_open);
146   klass->close = GST_DEBUG_FUNCPTR (gst_vulkan_window_default_close);
147
148   gst_vulkan_window_signals[SIGNAL_CLOSE] =
149       g_signal_new ("close", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0,
150       (GSignalAccumulator) _accum_logical_and, NULL, NULL, G_TYPE_BOOLEAN, 0);
151
152   gst_vulkan_window_signals[SIGNAL_DRAW] =
153       g_signal_new ("draw", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0,
154       NULL, NULL, NULL, G_TYPE_NONE, 0);
155
156   G_OBJECT_CLASS (klass)->finalize = gst_vulkan_window_finalize;
157
158   _init_debug ();
159 }
160
161 /**
162  * gst_vulkan_window_new:
163  * @display: a #GstGLDisplay
164  *
165  * Returns: (transfer full): a new #GstVulkanWindow using @display's connection
166  *
167  * Since: 1.10
168  */
169 GstVulkanWindow *
170 gst_vulkan_window_new (GstVulkanDisplay * display)
171 {
172   GstVulkanWindow *window = NULL;
173   const gchar *user_choice;
174
175   g_return_val_if_fail (display != NULL, NULL);
176
177   _init_debug ();
178
179   user_choice = g_getenv ("GST_VULKAN_WINDOW");
180   GST_INFO ("creating a window, user choice:%s", user_choice);
181 #if GST_VULKAN_HAVE_WINDOW_X11
182   if (!window && (!user_choice || g_strstr_len (user_choice, 3, "x11")))
183     window = GST_VULKAN_WINDOW (gst_vulkan_window_x11_new (display));
184 #endif
185 #if GST_VULKAN_HAVE_WINDOW_XCB
186   if (!window && (!user_choice || g_strstr_len (user_choice, 3, "xcb")))
187     window = GST_VULKAN_WINDOW (gst_vulkan_window_xcb_new (display));
188 #endif
189 #if GST_VULKAN_HAVE_WINDOW_WAYLAND
190   if (!window && (!user_choice || g_strstr_len (user_choice, 7, "wayland")))
191     window = GST_VULKAN_WINDOW (gst_vulkan_window_wayland_new (display));
192 #endif
193 #if GST_VULKAN_HAVE_WINDOW_COCOA
194   if (!window && (!user_choice || g_strstr_len (user_choice, 5, "cocoa")))
195     window = GST_VULKAN_WINDOW (gst_vulkan_window_cocoa_new (display));
196 #endif
197 #if GST_VULKAN_HAVE_WINDOW_IOS
198   if (!window && (!user_choice || g_strstr_len (user_choice, 3, "ios")))
199     window = GST_VULKAN_WINDOW (gst_vulkan_window_ios_new (display));
200 #endif
201   if (!window) {
202     /* subclass returned a NULL window */
203     GST_WARNING ("Could not create window. user specified %s, creating dummy"
204         " window", user_choice ? user_choice : "(null)");
205
206     window = GST_VULKAN_WINDOW (gst_vulkan_dummy_window_new ());
207   }
208
209   window->display = gst_object_ref (display);
210
211   return window;
212 }
213
214 static void
215 gst_vulkan_window_finalize (GObject * object)
216 {
217   GstVulkanWindow *window = GST_VULKAN_WINDOW (object);
218
219   gst_object_unref (window->display);
220
221   G_OBJECT_CLASS (gst_vulkan_window_parent_class)->finalize (object);
222 }
223
224 GstVulkanDisplay *
225 gst_vulkan_window_get_display (GstVulkanWindow * window)
226 {
227   g_return_val_if_fail (GST_IS_VULKAN_WINDOW (window), NULL);
228
229   return gst_object_ref (window->display);
230 }
231
232 VkSurfaceKHR
233 gst_vulkan_window_get_surface (GstVulkanWindow * window, GError ** error)
234 {
235   GstVulkanWindowClass *klass;
236
237   g_return_val_if_fail (GST_IS_VULKAN_WINDOW (window), (VkSurfaceKHR) 0);
238   klass = GST_VULKAN_WINDOW_GET_CLASS (window);
239   g_return_val_if_fail (klass->get_surface != NULL, (VkSurfaceKHR) 0);
240
241   return klass->get_surface (window, error);
242 }
243
244 gboolean
245 gst_vulkan_window_get_presentation_support (GstVulkanWindow * window,
246     GstVulkanDevice * device, guint32 queue_family_idx)
247 {
248   GstVulkanWindowClass *klass;
249
250   g_return_val_if_fail (GST_IS_VULKAN_WINDOW (window), FALSE);
251   klass = GST_VULKAN_WINDOW_GET_CLASS (window);
252   g_return_val_if_fail (klass->get_presentation_support != NULL, FALSE);
253
254   return klass->get_presentation_support (window, device, queue_family_idx);
255 }
256
257 gboolean
258 gst_vulkan_window_open (GstVulkanWindow * window, GError ** error)
259 {
260   GstVulkanWindowClass *klass;
261
262   g_return_val_if_fail (GST_IS_VULKAN_WINDOW (window), FALSE);
263   klass = GST_VULKAN_WINDOW_GET_CLASS (window);
264   g_return_val_if_fail (klass->open != NULL, FALSE);
265
266   return klass->open (window, error);
267 }
268
269 void
270 gst_vulkan_window_close (GstVulkanWindow * window)
271 {
272   GstVulkanWindowClass *klass;
273   gboolean to_close;
274
275   g_return_if_fail (GST_IS_VULKAN_WINDOW (window));
276   klass = GST_VULKAN_WINDOW_GET_CLASS (window);
277   g_return_if_fail (klass->close != NULL);
278
279   g_signal_emit (window, gst_vulkan_window_signals[SIGNAL_CLOSE], 0, &to_close);
280
281   if (to_close)
282     klass->close (window);
283 }
284
285 void
286 gst_vulkan_window_resize (GstVulkanWindow * window, gint width, gint height)
287 {
288   g_return_if_fail (GST_IS_VULKAN_WINDOW (window));
289
290   window->priv->surface_width = width;
291   window->priv->surface_height = height;
292
293   /* XXX: possibly queue a resize/redraw */
294 }
295
296 void
297 gst_vulkan_window_redraw (GstVulkanWindow * window)
298 {
299   g_return_if_fail (GST_IS_VULKAN_WINDOW (window));
300
301   g_signal_emit (window, gst_vulkan_window_signals[SIGNAL_DRAW], 0);
302 }
303
304 void
305 gst_vulkan_window_set_window_handle (GstVulkanWindow * window, guintptr handle)
306 {
307   GstVulkanWindowClass *klass;
308
309   g_return_if_fail (GST_IS_VULKAN_WINDOW (window));
310   klass = GST_VULKAN_WINDOW_GET_CLASS (window);
311
312   if (!klass->set_window_handle) {
313     if (handle)
314       g_warning ("%s does not implement the set_window_handle vfunc. "
315           "Output will not be embedded into the specified surface.",
316           GST_OBJECT_NAME (window));
317   } else {
318     klass->set_window_handle (window, handle);
319   }
320 }
321
322 GType gst_vulkan_dummy_window_get_type (void);
323 G_DEFINE_TYPE (GstVulkanDummyWindow, gst_vulkan_dummy_window,
324     GST_TYPE_VULKAN_WINDOW);
325
326 static void
327 gst_vulkan_dummy_window_class_init (GstVulkanDummyWindowClass * klass)
328 {
329 }
330
331 static void
332 gst_vulkan_dummy_window_init (GstVulkanDummyWindow * dummy)
333 {
334   dummy->handle = 0;
335 }
336
337 GstVulkanDummyWindow *
338 gst_vulkan_dummy_window_new (void)
339 {
340   GstVulkanDummyWindow *window;
341
342   window = g_object_new (gst_vulkan_dummy_window_get_type (), NULL);
343   gst_object_ref_sink (window);
344
345   return window;
346 }