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 "vkdisplay_xcb.h"
26 #include "xcb_event_source.h"
27
28 #define GST_CAT_DEFAULT gst_vulkan_display_debug
29 GST_DEBUG_CATEGORY_STATIC (gst_vulkan_display_debug);
30
31 G_DEFINE_TYPE (GstVulkanDisplayXCB, gst_vulkan_display_xcb,
32 GST_TYPE_VULKAN_DISPLAY);
33
34 static void gst_vulkan_display_xcb_finalize (GObject * object);
35 static gpointer gst_vulkan_display_xcb_get_handle (GstVulkanDisplay * display);
36
37 static void
gst_vulkan_display_xcb_class_init(GstVulkanDisplayXCBClass * klass)38 gst_vulkan_display_xcb_class_init (GstVulkanDisplayXCBClass * klass)
39 {
40 GST_VULKAN_DISPLAY_CLASS (klass)->get_handle =
41 GST_DEBUG_FUNCPTR (gst_vulkan_display_xcb_get_handle);
42
43 G_OBJECT_CLASS (klass)->finalize = gst_vulkan_display_xcb_finalize;
44 }
45
46 static void
gst_vulkan_display_xcb_init(GstVulkanDisplayXCB * display_xcb)47 gst_vulkan_display_xcb_init (GstVulkanDisplayXCB * display_xcb)
48 {
49 GstVulkanDisplay *display = (GstVulkanDisplay *) display_xcb;
50
51 display->type = GST_VULKAN_DISPLAY_TYPE_XCB;
52 display_xcb->foreign_display = FALSE;
53 }
54
55 static void
gst_vulkan_display_xcb_finalize(GObject * object)56 gst_vulkan_display_xcb_finalize (GObject * object)
57 {
58 GstVulkanDisplayXCB *display_xcb = GST_VULKAN_DISPLAY_XCB (object);
59
60 G_OBJECT_CLASS (gst_vulkan_display_xcb_parent_class)->finalize (object);
61
62 if (!display_xcb->foreign_display && display_xcb->connection)
63 xcb_disconnect (display_xcb->connection);
64 display_xcb->connection = NULL;
65 }
66
67 static xcb_screen_t *
_get_screen_from_connection(xcb_connection_t * connection,int screen_no)68 _get_screen_from_connection (xcb_connection_t * connection, int screen_no)
69 {
70 const xcb_setup_t *setup;
71 xcb_screen_iterator_t iter;
72
73 setup = xcb_get_setup (connection);
74 iter = xcb_setup_roots_iterator (setup);
75 while (screen_no-- > 0)
76 xcb_screen_next (&iter);
77
78 return iter.data;
79 }
80
81 /**
82 * gst_vulkan_display_xcb_new:
83 * @name: (allow-none): a display name
84 *
85 * Create a new #GstVulkanDisplayXCB from the xcb display name. See XOpenDisplay()
86 * for details on what is a valid name.
87 *
88 * Returns: (transfer full): a new #GstVulkanDisplayXCB or %NULL
89 */
90 GstVulkanDisplayXCB *
gst_vulkan_display_xcb_new(const gchar * name)91 gst_vulkan_display_xcb_new (const gchar * name)
92 {
93 xcb_connection_t *connection;
94 GstVulkanDisplayXCB *ret;
95 int screen_no = 0;
96
97 GST_DEBUG_CATEGORY_GET (gst_vulkan_display_debug, "gldisplay");
98
99 connection = xcb_connect (NULL, &screen_no);
100 if (connection == NULL || xcb_connection_has_error (connection)) {
101 GST_ERROR ("Failed to open XCB display connection with name, \'%s\'", name);
102 return NULL;
103 }
104
105 ret = gst_vulkan_display_xcb_new_with_connection (connection, screen_no);
106 GST_VULKAN_DISPLAY (ret)->event_source = xcb_event_source_new (ret);
107 g_source_attach (GST_VULKAN_DISPLAY (ret)->event_source,
108 GST_VULKAN_DISPLAY (ret)->main_context);
109 ret->foreign_display = FALSE;
110
111 return ret;
112 }
113
114 /**
115 * gst_vulkan_display_xcb_new_with_connection:
116 * @display: an existing, xcb display
117 *
118 * Creates a new display connection from a XCB Display.
119 *
120 * Returns: (transfer full): a new #GstVulkanDisplayXCB
121 */
122 GstVulkanDisplayXCB *
gst_vulkan_display_xcb_new_with_connection(xcb_connection_t * connection,int screen_no)123 gst_vulkan_display_xcb_new_with_connection (xcb_connection_t * connection,
124 int screen_no)
125 {
126 GstVulkanDisplayXCB *ret;
127
128 g_return_val_if_fail (connection != NULL, NULL);
129
130 GST_DEBUG_CATEGORY_GET (gst_vulkan_display_debug, "gldisplay");
131
132 ret = g_object_new (GST_TYPE_VULKAN_DISPLAY_XCB, NULL);
133 gst_object_ref_sink (ret);
134
135 ret->connection = connection;
136 ret->screen = _get_screen_from_connection (connection, screen_no);
137 ret->root_window = ret->screen->root;
138 ret->foreign_display = TRUE;
139
140 return ret;
141 }
142
143 static gpointer
gst_vulkan_display_xcb_get_handle(GstVulkanDisplay * display)144 gst_vulkan_display_xcb_get_handle (GstVulkanDisplay * display)
145 {
146 return (gpointer) GST_VULKAN_DISPLAY_XCB_CONNECTION (display);
147 }
148