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