gst: don't use volatile to mean atomic
[platform/upstream/gstreamer.git] / gst-libs / gst / vulkan / gstvkwindow.c
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 /**
22  * SECTION:vkwindow
23  * @title: GstVulkanWindow
24  * @short_description: window/surface abstraction
25  * @see_also: #GstVulkanDisplay, #GstVulkanSwapper
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 "gstvkwindow.h"
39
40 #if GST_VULKAN_HAVE_WINDOW_XCB
41 #include "xcb/gstvkwindow_xcb.h"
42 #endif
43 #if GST_VULKAN_HAVE_WINDOW_WAYLAND
44 #include "wayland/gstvkwindow_wayland.h"
45 #endif
46 #if GST_VULKAN_HAVE_WINDOW_COCOA
47 #include "cocoa/gstvkwindow_cocoa.h"
48 #endif
49 #if GST_VULKAN_HAVE_WINDOW_IOS
50 #include "ios/gstvkwindow_ios.h"
51 #endif
52 #if GST_VULKAN_HAVE_WINDOW_WIN32
53 #include "win32/gstvkwindow_win32.h"
54 #endif
55 #if GST_VULKAN_HAVE_WINDOW_ANDROID
56 #include "android/gstvkwindow_android.h"
57 #endif
58
59 #define GST_CAT_DEFAULT gst_vulkan_window_debug
60 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
61
62 struct _GstVulkanWindowPrivate
63 {
64   guint surface_width;
65   guint surface_height;
66 };
67
68 #define GET_PRIV(window) gst_vulkan_window_get_instance_private (window)
69
70 #define gst_vulkan_window_parent_class parent_class
71 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GstVulkanWindow, gst_vulkan_window,
72     GST_TYPE_OBJECT);
73
74 static void gst_vulkan_window_finalize (GObject * object);
75
76 typedef struct _GstVulkanDummyWindow
77 {
78   GstVulkanWindow parent;
79
80   guintptr handle;
81 } GstVulkanDummyWindow;
82
83 typedef struct _GstVulkanDummyWindowCass
84 {
85   GstVulkanWindowClass parent;
86 } GstVulkanDummyWindowClass;
87
88 GstVulkanDummyWindow *gst_vulkan_dummy_window_new (void);
89
90 enum
91 {
92   PROP_0,
93   PROP_DISPLAY,
94 };
95
96 enum
97 {
98   SIGNAL_0,
99   SIGNAL_CLOSE,
100   SIGNAL_DRAW,
101   SIGNAL_RESIZE,
102   SIGNAL_MOUSE_EVENT,
103   SIGNAL_KEY_EVENT,
104   LAST_SIGNAL
105 };
106
107 static guint gst_vulkan_window_signals[LAST_SIGNAL] = { 0 };
108
109 static gboolean
110 _accum_logical_and (GSignalInvocationHint * ihint, GValue * return_accu,
111     const GValue * handler_return, gpointer data)
112 {
113   gboolean val = g_value_get_boolean (handler_return);
114   gboolean val2 = g_value_get_boolean (return_accu);
115
116   g_value_set_boolean (return_accu, val && val2);
117
118   return TRUE;
119 }
120
121 GQuark
122 gst_vulkan_window_error_quark (void)
123 {
124   return g_quark_from_static_string ("gst-gl-window-error-quark");
125 }
126
127 static gboolean
128 gst_vulkan_window_default_open (GstVulkanWindow * window, GError ** error)
129 {
130   return TRUE;
131 }
132
133 static void
134 gst_vulkan_window_default_close (GstVulkanWindow * window)
135 {
136 }
137
138 static void
139 _init_debug (void)
140 {
141   static gsize _init = 0;
142
143   if (g_once_init_enter (&_init)) {
144     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "vulkanwindow", 0,
145         "Vulkan Window");
146     g_once_init_leave (&_init, 1);
147   }
148 }
149
150 static void
151 gst_vulkan_window_set_property (GObject * object, guint prop_id,
152     const GValue * value, GParamSpec * pspec)
153 {
154   GstVulkanWindow *window = GST_VULKAN_WINDOW (object);
155
156   switch (prop_id) {
157     case PROP_DISPLAY:
158       window->display = g_value_dup_object (value);
159       break;
160     default:
161       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
162       break;
163   }
164 }
165
166 static void
167 gst_vulkan_window_get_property (GObject * object, guint prop_id,
168     GValue * value, GParamSpec * pspec)
169 {
170   GstVulkanWindow *window = GST_VULKAN_WINDOW (object);
171
172   switch (prop_id) {
173     case PROP_DISPLAY:
174       g_value_set_object (value, window->display);
175       break;
176     default:
177       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
178       break;
179   }
180 }
181
182 static void
183 gst_vulkan_window_init (GstVulkanWindow * window)
184 {
185 }
186
187 static void
188 gst_vulkan_window_class_init (GstVulkanWindowClass * klass)
189 {
190   GObjectClass *gobject_class = (GObjectClass *) klass;
191
192   klass->open = GST_DEBUG_FUNCPTR (gst_vulkan_window_default_open);
193   klass->close = GST_DEBUG_FUNCPTR (gst_vulkan_window_default_close);
194
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);
198
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);
202
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);
206
207   /**
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
214    *
215    * Will be emitted when a mouse event is received by the #GstVulkanWindow.
216    *
217    * Since: 1.18
218    */
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);
223
224   /**
225    * GstVulkanWindow::key-event:
226    * @object: the #GstVulkanWindow
227    * @id: the name of the event
228    * @key: the id of the key pressed
229    *
230    * Will be emitted when a key event is received by the #GstVulkanWindow.
231    *
232    * Since: 1.18
233    */
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,
237       G_TYPE_STRING);
238
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;
242
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));
247
248   _init_debug ();
249 }
250
251 /**
252  * gst_vulkan_window_new:
253  * @display: a #GstVulkanDisplay
254  *
255  * Returns: (transfer full): a new #GstVulkanWindow using @display's connection
256  *
257  * Since: 1.18
258  */
259 GstVulkanWindow *
260 gst_vulkan_window_new (GstVulkanDisplay * display)
261 {
262   GstVulkanWindow *window = NULL;
263   const gchar *user_choice;
264
265   g_return_val_if_fail (display != NULL, NULL);
266
267   _init_debug ();
268
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));
274 #endif
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));
278 #endif
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));
282 #endif
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));
286 #endif
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));
290 #endif
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));
294 #endif
295   if (!window) {
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)");
299
300     window = GST_VULKAN_WINDOW (gst_vulkan_dummy_window_new ());
301   }
302
303   window->display = gst_object_ref (display);
304
305   return window;
306 }
307
308 static void
309 gst_vulkan_window_finalize (GObject * object)
310 {
311   GstVulkanWindow *window = GST_VULKAN_WINDOW (object);
312
313   gst_object_unref (window->display);
314
315   G_OBJECT_CLASS (gst_vulkan_window_parent_class)->finalize (object);
316 }
317
318 /**
319  * gst_vulkan_window_get_display:
320  * @window: a #GstVulkanWindow
321  *
322  * Returns: (transfer full): the #GstVulkanDisplay for @window
323  *
324  * Since: 1.18
325  */
326 GstVulkanDisplay *
327 gst_vulkan_window_get_display (GstVulkanWindow * window)
328 {
329   g_return_val_if_fail (GST_IS_VULKAN_WINDOW (window), NULL);
330
331   return gst_object_ref (window->display);
332 }
333
334 /**
335  * gst_vulkan_window_get_surface: (skip)
336  * @window: a #GstVulkanWindow
337  * @error: a #GError
338  *
339  * Returns: the VkSurface for displaying into.  The caller is responsible for
340  *     calling `VkDestroySurface` on the returned surface.
341  *
342  * Since: 1.18
343  */
344 VkSurfaceKHR
345 gst_vulkan_window_get_surface (GstVulkanWindow * window, GError ** error)
346 {
347   GstVulkanWindowClass *klass;
348
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);
352
353   return klass->get_surface (window, error);
354 }
355
356 /**
357  * gst_vulkan_window_get_presentation_support:
358  * @window: a #GstVulkanWindow
359  * @device: a #GstVulkanDevice
360  * @queue_family_idx: the queue family
361  *
362  * Returns: whether the given combination of @window, @device and
363  *          @queue_family_idx supports presentation
364  *
365  * Since: 1.18
366  */
367 gboolean
368 gst_vulkan_window_get_presentation_support (GstVulkanWindow * window,
369     GstVulkanDevice * device, guint32 queue_family_idx)
370 {
371   GstVulkanWindowClass *klass;
372
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);
376
377   return klass->get_presentation_support (window, device, queue_family_idx);
378 }
379
380 /**
381  * gst_vulkan_window_open:
382  * @window: a #GstVulkanWindow
383  * @error: a #GError
384  *
385  * Returns: whether @window could be successfully opened
386  *
387  * Since: 1.18
388  */
389 gboolean
390 gst_vulkan_window_open (GstVulkanWindow * window, GError ** error)
391 {
392   GstVulkanWindowClass *klass;
393
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);
397
398   return klass->open (window, error);
399 }
400
401 /**
402  * gst_vulkan_window_close:
403  * @window: a #GstVulkanWindow
404  *
405  * Attempt to close the window.
406  *
407  * Since: 1.18
408  */
409 void
410 gst_vulkan_window_close (GstVulkanWindow * window)
411 {
412   GstVulkanWindowClass *klass;
413   gboolean to_close;
414
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);
418
419   g_signal_emit (window, gst_vulkan_window_signals[SIGNAL_CLOSE], 0, &to_close);
420
421   if (to_close)
422     klass->close (window);
423 }
424
425 /**
426  * gst_vulkan_window_resize:
427  * @window: a #GstVulkanWindow
428  * @width: the new width
429  * @height: the new height
430  *
431  * Resize the output surface.
432  *
433  * Currently intended for subclasses to update internal state.
434  *
435  * Since: 1.18
436  */
437 void
438 gst_vulkan_window_resize (GstVulkanWindow * window, gint width, gint height)
439 {
440   GstVulkanWindowPrivate *priv;
441
442   g_return_if_fail (GST_IS_VULKAN_WINDOW (window));
443
444   priv = GET_PRIV (window);
445
446   priv->surface_width = width;
447   priv->surface_height = height;
448
449   g_signal_emit (window, gst_vulkan_window_signals[SIGNAL_RESIZE], 0, width,
450       height);
451 }
452
453 /**
454  * gst_vulkan_window_redraw:
455  * @window: a #GstVulkanWindow
456  *
457  * Ask the @window to redraw its contents
458  *
459  * Since: 1.18
460  */
461 void
462 gst_vulkan_window_redraw (GstVulkanWindow * window)
463 {
464   g_return_if_fail (GST_IS_VULKAN_WINDOW (window));
465
466   g_signal_emit (window, gst_vulkan_window_signals[SIGNAL_DRAW], 0);
467 }
468
469 void
470 gst_vulkan_window_set_window_handle (GstVulkanWindow * window, guintptr handle)
471 {
472   GstVulkanWindowClass *klass;
473
474   g_return_if_fail (GST_IS_VULKAN_WINDOW (window));
475   klass = GST_VULKAN_WINDOW_GET_CLASS (window);
476
477   if (!klass->set_window_handle) {
478     if (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));
482   } else {
483     klass->set_window_handle (window, handle);
484   }
485 }
486
487 void
488 gst_vulkan_window_get_surface_dimensions (GstVulkanWindow * window,
489     guint * width, guint * height)
490 {
491   GstVulkanWindowPrivate *priv;
492   GstVulkanWindowClass *klass;
493
494   g_return_if_fail (GST_IS_VULKAN_WINDOW (window));
495   klass = GST_VULKAN_WINDOW_GET_CLASS (window);
496   priv = GET_PRIV (window);
497
498   if (klass->get_surface_dimensions) {
499     klass->get_surface_dimensions (window, width, height);
500   } else {
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;
505   }
506 }
507
508 void
509 gst_vulkan_window_send_key_event (GstVulkanWindow * window,
510     const char *event_type, const char *key_str)
511 {
512   g_return_if_fail (GST_IS_VULKAN_WINDOW (window));
513
514   g_signal_emit (window, gst_vulkan_window_signals[SIGNAL_KEY_EVENT], 0,
515       event_type, key_str);
516 }
517
518 void
519 gst_vulkan_window_send_mouse_event (GstVulkanWindow * window,
520     const char *event_type, int button, double posx, double posy)
521 {
522   g_return_if_fail (GST_IS_VULKAN_WINDOW (window));
523
524   g_signal_emit (window, gst_vulkan_window_signals[SIGNAL_MOUSE_EVENT], 0,
525       event_type, button, posx, posy);
526 }
527
528 /**
529  * gst_vulkan_window_handle_events:
530  * @window: a #GstVulkanWindow
531  * @handle_events: a #gboolean indicating if events should be handled or not.
532  *
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
537  * from the @window.
538  *
539  * Since: 1.18
540  */
541 void
542 gst_vulkan_window_handle_events (GstVulkanWindow * window,
543     gboolean handle_events)
544 {
545   GstVulkanWindowClass *window_class;
546
547   g_return_if_fail (GST_IS_VULKAN_WINDOW (window));
548   window_class = GST_VULKAN_WINDOW_GET_CLASS (window);
549
550   if (window_class->handle_events)
551     window_class->handle_events (window, handle_events);
552 }
553
554 GType gst_vulkan_dummy_window_get_type (void);
555 G_DEFINE_TYPE (GstVulkanDummyWindow, gst_vulkan_dummy_window,
556     GST_TYPE_VULKAN_WINDOW);
557
558 static void
559 gst_vulkan_dummy_window_class_init (GstVulkanDummyWindowClass * klass)
560 {
561 }
562
563 static void
564 gst_vulkan_dummy_window_init (GstVulkanDummyWindow * dummy)
565 {
566   dummy->handle = 0;
567 }
568
569 GstVulkanDummyWindow *
570 gst_vulkan_dummy_window_new (void)
571 {
572   GstVulkanDummyWindow *window;
573
574   window = g_object_new (gst_vulkan_dummy_window_get_type (), NULL);
575   gst_object_ref_sink (window);
576
577   return window;
578 }