b0f1ab4d0f7a84126dfa61f8aaacc06f2c0b329f
[platform/upstream/gstreamer.git] / gst-libs / gst / vulkan / xcb / gstvkwindow_xcb.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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/gst.h>
26 #include <locale.h>
27
28 #include "gstvkwindow_xcb.h"
29 #include "gstvkdisplay_xcb.h"
30
31 #include <xkbcommon/xkbcommon.h>
32 #include <xkbcommon/xkbcommon-x11.h>
33
34 #define GET_PRIV(o) gst_vulkan_window_xcb_get_instance_private (o)
35
36 #define GST_CAT_DEFAULT gst_vulkan_window_xcb_debug
37 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
38
39 static void
40 _init_debug (void)
41 {
42   static volatile gsize _init = 0;
43
44   if (g_once_init_enter (&_init)) {
45     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "vulkanwindowxcb", 0,
46         "Vulkan XCB Window");
47     g_once_init_leave (&_init, 1);
48   }
49 }
50
51 enum
52 {
53   PROP_0,
54 };
55
56 struct _GstVulkanWindowXCBPrivate
57 {
58   gboolean activate;
59   gboolean activate_result;
60
61   gint preferred_width;
62   gint preferred_height;
63
64   xcb_intern_atom_reply_t *atom_wm_delete_window;
65   gboolean handle_events;
66
67   guint8 first_xkb_event;
68   gint32 kbd_device_id;
69   struct xkb_context *xkb_ctx;
70   struct xkb_keymap *xkb_keymap;
71   struct xkb_state *xkb_state;
72 };
73
74 #define gst_vulkan_window_xcb_parent_class parent_class
75 G_DEFINE_TYPE_WITH_CODE (GstVulkanWindowXCB, gst_vulkan_window_xcb,
76     GST_TYPE_VULKAN_WINDOW, G_ADD_PRIVATE (GstVulkanWindowXCB) _init_debug ());
77
78 static VkSurfaceKHR gst_vulkan_window_xcb_get_surface (GstVulkanWindow * window,
79     GError ** error);
80 static gboolean gst_vulkan_window_xcb_get_presentation_support (GstVulkanWindow
81     * window, GstVulkanDevice * device, guint32 queue_family_idx);
82 static gboolean gst_vulkan_window_xcb_open (GstVulkanWindow * window,
83     GError ** error);
84 static void gst_vulkan_window_xcb_close (GstVulkanWindow * window);
85 static void gst_vulkan_window_xcb_handle_events (GstVulkanWindow * window,
86     gboolean handle_events);
87
88 static void
89 gst_vulkan_window_xcb_finalize (GObject * object)
90 {
91   G_OBJECT_CLASS (parent_class)->finalize (object);
92 }
93
94 static void
95 gst_vulkan_window_xcb_class_init (GstVulkanWindowXCBClass * klass)
96 {
97   GObjectClass *obj_class = G_OBJECT_CLASS (klass);
98   GstVulkanWindowClass *window_class = (GstVulkanWindowClass *) klass;
99
100   obj_class->finalize = gst_vulkan_window_xcb_finalize;
101
102   window_class->open = GST_DEBUG_FUNCPTR (gst_vulkan_window_xcb_open);
103   window_class->close = GST_DEBUG_FUNCPTR (gst_vulkan_window_xcb_close);
104   window_class->get_surface = gst_vulkan_window_xcb_get_surface;
105   window_class->handle_events = gst_vulkan_window_xcb_handle_events;
106   window_class->get_presentation_support =
107       gst_vulkan_window_xcb_get_presentation_support;
108 }
109
110 static void
111 gst_vulkan_window_xcb_init (GstVulkanWindowXCB * window)
112 {
113   GstVulkanWindowXCBPrivate *priv = GET_PRIV (window);
114
115   priv->handle_events = TRUE;
116 }
117
118 /* Must be called in the gl thread */
119 GstVulkanWindowXCB *
120 gst_vulkan_window_xcb_new (GstVulkanDisplay * display)
121 {
122   GstVulkanWindowXCB *window;
123
124   _init_debug ();
125
126   if ((gst_vulkan_display_get_handle_type (display) &
127           GST_VULKAN_DISPLAY_TYPE_XCB)
128       == GST_VULKAN_DISPLAY_TYPE_NONE) {
129     GST_INFO ("Wrong display type %u for this window type %u", display->type,
130         GST_VULKAN_DISPLAY_TYPE_XCB);
131     return NULL;
132   }
133
134   window = g_object_new (GST_TYPE_VULKAN_WINDOW_XCB, NULL);
135   gst_object_ref_sink (window);
136
137   return window;
138 }
139
140 static void
141 gst_vulkan_window_xcb_show (GstVulkanWindow * window)
142 {
143   GstVulkanWindowXCB *window_xcb = GST_VULKAN_WINDOW_XCB (window);
144   GstVulkanDisplayXCB *display_xcb = GST_VULKAN_DISPLAY_XCB (window->display);
145   xcb_connection_t *connection =
146       GST_VULKAN_DISPLAY_XCB_CONNECTION (display_xcb);
147
148   if (!window_xcb->visible) {
149     xcb_map_window (connection, window_xcb->win_id);
150     xcb_flush (connection);
151     window_xcb->visible = TRUE;
152   }
153 }
154
155 static void
156 gst_vulkan_window_xcb_hide (GstVulkanWindow * window)
157 {
158   GstVulkanWindowXCB *window_xcb = GST_VULKAN_WINDOW_XCB (window);
159   GstVulkanDisplayXCB *display_xcb = GST_VULKAN_DISPLAY_XCB (window->display);
160   xcb_connection_t *connection =
161       GST_VULKAN_DISPLAY_XCB_CONNECTION (display_xcb);
162
163   if (window_xcb->visible) {
164     xcb_unmap_window (connection, window_xcb->win_id);
165     window_xcb->visible = FALSE;
166   }
167 }
168
169 static gboolean
170 init_keyboard (GstVulkanWindowXCB * window_xcb)
171 {
172   GstVulkanWindowXCBPrivate *priv = GET_PRIV (window_xcb);
173   GstVulkanWindow *window = GST_VULKAN_WINDOW (window_xcb);
174   GstVulkanDisplayXCB *display_xcb = GST_VULKAN_DISPLAY_XCB (window->display);
175   xcb_connection_t *connection =
176       GST_VULKAN_DISPLAY_XCB_CONNECTION (display_xcb);
177   int ret;
178
179   ret = xkb_x11_setup_xkb_extension (connection,
180       XKB_X11_MIN_MAJOR_XKB_VERSION,
181       XKB_X11_MIN_MINOR_XKB_VERSION,
182       XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS,
183       NULL, NULL, &priv->first_xkb_event, NULL);
184   if (!ret) {
185     GST_ERROR_OBJECT (window_xcb, "Couldn't setup XKB extension\n");
186     return FALSE;;
187   }
188
189   priv->xkb_ctx = xkb_context_new (0);
190   if (!priv->xkb_ctx)
191     return FALSE;
192
193   priv->kbd_device_id = xkb_x11_get_core_keyboard_device_id (connection);
194
195   priv->xkb_keymap = xkb_x11_keymap_new_from_device (priv->xkb_ctx,
196       connection, priv->kbd_device_id, XKB_KEYMAP_COMPILE_NO_FLAGS);
197   if (!priv->xkb_keymap)
198     return FALSE;
199   priv->xkb_state = xkb_x11_state_new_from_device (priv->xkb_keymap,
200       connection, priv->kbd_device_id);
201   if (!priv->xkb_state)
202     return FALSE;
203
204   return TRUE;
205 }
206
207 gboolean
208 gst_vulkan_window_xcb_create_window (GstVulkanWindowXCB * window_xcb)
209 {
210   GstVulkanWindowXCBPrivate *priv = GET_PRIV (window_xcb);
211   GstVulkanDisplayXCB *display_xcb;
212   xcb_connection_t *connection;
213   xcb_screen_t *screen;
214   xcb_window_t root_window;
215   uint32_t value_mask, value_list[32];
216   xcb_intern_atom_cookie_t cookie, cookie2;
217   xcb_intern_atom_reply_t *reply, *reply2;
218 //  const gchar *title = "OpenGL renderer";
219   gint x = 0, y = 0, width = 320, height = 240;
220
221   display_xcb =
222       GST_VULKAN_DISPLAY_XCB (GST_VULKAN_WINDOW (window_xcb)->display);
223   connection = GST_VULKAN_DISPLAY_XCB_CONNECTION (display_xcb);
224   root_window = GST_VULKAN_DISPLAY_XCB_ROOT_WINDOW (display_xcb);
225   screen = GST_VULKAN_DISPLAY_XCB_SCREEN (display_xcb);
226
227   window_xcb->win_id = xcb_generate_id (connection);
228
229   value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
230   value_list[0] = screen->black_pixel;
231   value_list[1] =
232       XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE |
233       XCB_EVENT_MASK_STRUCTURE_NOTIFY;
234
235   xcb_create_window (connection, XCB_COPY_FROM_PARENT, window_xcb->win_id,
236       root_window, x, y, width, height, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
237       screen->root_visual, value_mask, value_list);
238
239   gst_vulkan_window_xcb_handle_events (GST_VULKAN_WINDOW (window_xcb),
240       priv->handle_events);
241
242   GST_LOG_OBJECT (window_xcb, "vulkan window id: %p",
243       (gpointer) (guintptr) window_xcb->win_id);
244   GST_LOG_OBJECT (window_xcb, "vulkan window props: x:%d y:%d", x, y);
245
246   /* Magic code that will send notification when window is destroyed */
247   cookie = xcb_intern_atom (connection, 1, 12, "WM_PROTOCOLS");
248   reply = xcb_intern_atom_reply (connection, cookie, 0);
249
250   cookie2 = xcb_intern_atom (connection, 0, 16, "WM_DELETE_WINDOW");
251   reply2 = xcb_intern_atom_reply (connection, cookie2, 0);
252
253   xcb_change_property (connection, XCB_PROP_MODE_REPLACE, window_xcb->win_id,
254       reply->atom, 4, 32, 1, &reply2->atom);
255   g_free (reply);
256   g_free (reply2);
257
258   init_keyboard (window_xcb);
259
260   gst_vulkan_window_xcb_show (GST_VULKAN_WINDOW (window_xcb));
261
262   return TRUE;
263 }
264
265 static VkSurfaceKHR
266 gst_vulkan_window_xcb_get_surface (GstVulkanWindow * window, GError ** error)
267 {
268   GstVulkanWindowXCB *window_xcb = GST_VULKAN_WINDOW_XCB (window);
269   VkXcbSurfaceCreateInfoKHR info = { 0, };
270   VkSurfaceKHR ret;
271   VkResult err;
272
273   info.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
274   info.pNext = NULL;
275   info.flags = 0;
276   info.connection = GST_VULKAN_DISPLAY_XCB_CONNECTION (window->display);
277   info.window = GST_VULKAN_WINDOW_XCB (window)->win_id;
278
279   if (!window_xcb->CreateXcbSurface)
280     window_xcb->CreateXcbSurface =
281         gst_vulkan_instance_get_proc_address (window->display->instance,
282         "vkCreateXcbSurfaceKHR");
283   if (!window_xcb->CreateXcbSurface) {
284     g_set_error_literal (error, GST_VULKAN_ERROR, VK_ERROR_FEATURE_NOT_PRESENT,
285         "Could not retrieve \"vkCreateXcbSurfaceKHR\" function pointer");
286     return NULL;
287   }
288
289   err =
290       window_xcb->CreateXcbSurface (window->display->instance->instance, &info,
291       NULL, &ret);
292   if (gst_vulkan_error_to_g_error (err, error, "vkCreateXcbSurfaceKHR") < 0)
293     return NULL;
294
295   return ret;
296 }
297
298 static gboolean
299 gst_vulkan_window_xcb_get_presentation_support (GstVulkanWindow * window,
300     GstVulkanDevice * device, guint32 queue_family_idx)
301 {
302   GstVulkanWindowXCB *window_xcb = GST_VULKAN_WINDOW_XCB (window);
303   VkPhysicalDevice gpu;
304   xcb_screen_t *screen;
305
306   screen = GST_VULKAN_DISPLAY_XCB_SCREEN (window->display);
307
308   if (!window_xcb->GetPhysicalDeviceXcbPresentationSupport)
309     window_xcb->GetPhysicalDeviceXcbPresentationSupport =
310         gst_vulkan_instance_get_proc_address (window->display->instance,
311         "vkGetPhysicalDeviceXcbPresentationSupportKHR");
312   if (!window_xcb->GetPhysicalDeviceXcbPresentationSupport) {
313     GST_WARNING_OBJECT (window, "Could not retrieve "
314         "\"vkGetPhysicalDeviceXcbPresentationSupportKHR\" " "function pointer");
315     return FALSE;
316   }
317
318   gpu = gst_vulkan_device_get_physical_device (device);
319   if (window_xcb->GetPhysicalDeviceXcbPresentationSupport (gpu,
320           queue_family_idx, GST_VULKAN_DISPLAY_XCB_CONNECTION (window->display),
321           screen->root_visual))
322     return TRUE;
323   return FALSE;
324 }
325
326 static gboolean
327 gst_vulkan_window_xcb_open (GstVulkanWindow * window, GError ** error)
328 {
329   GstVulkanWindowXCB *window_xcb = GST_VULKAN_WINDOW_XCB (window);
330   GstVulkanDisplayXCB *display_xcb;
331   xcb_connection_t *connection;
332
333   if (!GST_IS_VULKAN_DISPLAY_XCB (window->display)) {
334     g_set_error (error, GST_VULKAN_WINDOW_ERROR,
335         GST_VULKAN_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
336         "Cannot create an XCB window from a non-XCB display");
337     goto failure;
338   }
339
340   display_xcb = GST_VULKAN_DISPLAY_XCB (window->display);
341   connection = GST_VULKAN_DISPLAY_XCB_CONNECTION (display_xcb);
342   if (connection == NULL) {
343     g_set_error (error, GST_VULKAN_WINDOW_ERROR,
344         GST_VULKAN_WINDOW_ERROR_RESOURCE_UNAVAILABLE,
345         "Failed to connect to X display server with XCB");
346     goto failure;
347   }
348
349   if (!GST_VULKAN_WINDOW_CLASS (parent_class)->open (window, error))
350     return FALSE;
351
352   return gst_vulkan_window_xcb_create_window (window_xcb);
353
354 failure:
355   return FALSE;
356 }
357
358 static void
359 gst_vulkan_window_xcb_close (GstVulkanWindow * window)
360 {
361   GstVulkanWindowXCB *window_xcb = GST_VULKAN_WINDOW_XCB (window);
362   GstVulkanWindowXCBPrivate *priv = GET_PRIV (window_xcb);
363   GstVulkanDisplayXCB *display_xcb = (GstVulkanDisplayXCB *) window->display;
364   xcb_connection_t *connection =
365       GST_VULKAN_DISPLAY_XCB_CONNECTION (display_xcb);
366
367   if (connection) {
368     gst_vulkan_window_xcb_hide (window);
369
370     g_free (priv->atom_wm_delete_window);
371     priv->atom_wm_delete_window = NULL;
372     GST_DEBUG ("display receiver closed");
373   }
374
375   if (priv->xkb_state)
376     xkb_state_unref (priv->xkb_state);
377   priv->xkb_state = NULL;
378   if (priv->xkb_keymap)
379     xkb_keymap_unref (priv->xkb_keymap);
380   priv->xkb_keymap = NULL;
381   if (priv->xkb_ctx)
382     xkb_context_unref (priv->xkb_ctx);
383   priv->xkb_ctx = NULL;
384
385   GST_VULKAN_WINDOW_CLASS (parent_class)->close (window);
386 }
387
388 G_GNUC_INTERNAL
389     gboolean
390 gst_vulkan_window_xcb_handle_event (GstVulkanWindowXCB * window_xcb,
391     xcb_generic_event_t * event);
392
393 gboolean
394 gst_vulkan_window_xcb_handle_event (GstVulkanWindowXCB * window_xcb,
395     xcb_generic_event_t * event)
396 {
397   GstVulkanWindowXCBPrivate *priv = GET_PRIV (window_xcb);
398   GstVulkanDisplayXCB *display_xcb =
399       GST_VULKAN_DISPLAY_XCB (window_xcb->parent.display);
400   xcb_connection_t *connection =
401       GST_VULKAN_DISPLAY_XCB_CONNECTION (display_xcb);
402   uint8_t event_code = event->response_type & 0x7f;
403
404   switch (event_code) {
405     case XCB_CLIENT_MESSAGE:{
406       xcb_client_message_event_t *client_event;
407       xcb_intern_atom_cookie_t cookie;
408       xcb_intern_atom_reply_t *reply;
409
410       client_event = (xcb_client_message_event_t *) event;
411       cookie = xcb_intern_atom (connection, 0, 16, "WM_DELETE_WINDOW");
412       reply = xcb_intern_atom_reply (connection, cookie, 0);
413
414       if (client_event->data.data32[0] == reply->atom) {
415         GST_INFO_OBJECT (window_xcb, "Close requested");
416
417         gst_vulkan_window_close (GST_VULKAN_WINDOW (window_xcb));
418         gst_vulkan_display_remove_window (GST_VULKAN_DISPLAY (display_xcb),
419             GST_VULKAN_WINDOW (window_xcb));
420       }
421
422       g_free (reply);
423       break;
424     }
425     case XCB_CONFIGURE_NOTIFY:{
426       xcb_configure_notify_event_t *configure_event;
427
428       configure_event = (xcb_configure_notify_event_t *) event;
429
430       gst_vulkan_window_resize (GST_VULKAN_WINDOW (window_xcb),
431           configure_event->width, configure_event->height);
432       break;
433     }
434     case XCB_EXPOSE:{
435       xcb_expose_event_t *expose_event = (xcb_expose_event_t *) event;
436
437       /* non-zero means that other Expose follows
438        * so just wait for the last one
439        * in theory we should not receive non-zero because
440        * we have no sub areas here but just in case */
441       if (expose_event->count != 0)
442         break;
443
444       gst_vulkan_window_redraw (GST_VULKAN_WINDOW (window_xcb));
445       break;
446     }
447     case XCB_KEY_PRESS:
448     case XCB_KEY_RELEASE:{
449       xcb_key_press_event_t *kp = (xcb_key_press_event_t *) event;
450       int nsyms;
451       const xkb_keysym_t *syms;
452       const char *event_type_str;
453
454       if (!priv->xkb_state)
455         /* no xkb support. What year is this?! */
456         break;
457
458       nsyms = xkb_state_key_get_syms (priv->xkb_state, kp->detail, &syms);
459
460       if (event_code == XCB_KEY_PRESS)
461         event_type_str = "key-press";
462       else
463         event_type_str = "key-release";
464
465       /* TODO: compose states, keymap changes */
466       if (nsyms <= 0)
467         break;
468
469       for (int i = 0; i < nsyms; i++) {
470         char s[64];
471         xkb_keysym_get_name (syms[i], s, sizeof (s));
472         gst_vulkan_window_send_key_event (GST_VULKAN_WINDOW (window_xcb),
473             event_type_str, s);
474       }
475       break;
476     }
477     case XCB_BUTTON_PRESS:
478     case XCB_BUTTON_RELEASE:{
479       xcb_button_press_event_t *bp = (xcb_button_press_event_t *) event;
480       const gchar *event_type_str;
481
482       if (event_code == XCB_BUTTON_PRESS)
483         event_type_str = "mouse-button-press";
484       else
485         event_type_str = "mouse-button-release";
486
487       gst_vulkan_window_send_mouse_event (GST_VULKAN_WINDOW (window_xcb),
488           event_type_str, bp->detail, (double) bp->event_x,
489           (double) bp->event_y);
490       break;
491     }
492     case XCB_MOTION_NOTIFY:{
493       xcb_motion_notify_event_t *motion = (xcb_motion_notify_event_t *) event;
494
495       gst_vulkan_window_send_mouse_event (GST_VULKAN_WINDOW (window_xcb),
496           "mouse-move", 0, (double) motion->event_x, (double) motion->event_y);
497       break;
498     }
499     default:
500       GST_DEBUG ("unhandled XCB type: %u", event_code);
501       break;
502   }
503
504   return TRUE;
505 }
506
507 static void
508 gst_vulkan_window_xcb_handle_events (GstVulkanWindow * window,
509     gboolean handle_events)
510 {
511   GstVulkanDisplayXCB *display_xcb = GST_VULKAN_DISPLAY_XCB (window->display);
512   xcb_connection_t *connection =
513       GST_VULKAN_DISPLAY_XCB_CONNECTION (display_xcb);
514   GstVulkanWindowXCB *window_xcb = GST_VULKAN_WINDOW_XCB (window);
515   GstVulkanWindowXCBPrivate *priv = GET_PRIV (window_xcb);
516
517   priv->handle_events = handle_events;
518
519   if (window_xcb->win_id) {
520     guint32 events;
521
522     events = XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_EXPOSURE
523         | XCB_EVENT_MASK_VISIBILITY_CHANGE;
524     if (handle_events) {
525       events |= XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_EXPOSURE
526           | XCB_EVENT_MASK_VISIBILITY_CHANGE | XCB_EVENT_MASK_POINTER_MOTION
527           | XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE
528           | XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_KEY_RELEASE;
529     }
530     xcb_change_window_attributes (connection,
531         window_xcb->win_id, XCB_CW_EVENT_MASK, &events);
532   }
533 }