2 * gstvaapidisplay_x11.c - VA/X11 display abstraction
4 * Copyright (C) 2010-2011 Splitted-Desktop Systems
5 * Author: Gwenole Beauchesne <gwenole.beauchesne@splitted-desktop.com>
6 * Copyright (C) 2011-2013 Intel Corporation
7 * Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301 USA
26 * SECTION:gstvaapidisplay_x11
27 * @short_description: VA/X11 display abstraction
31 #include "gstvaapiutils.h"
32 #include "gstvaapidisplay_priv.h"
33 #include "gstvaapidisplay_x11.h"
34 #include "gstvaapidisplay_x11_priv.h"
35 #include "gstvaapiwindow_x11.h"
38 # include <X11/extensions/Xrandr.h>
41 #define DEBUG_VAAPI_DISPLAY 1
42 #include "gstvaapidebug.h"
44 G_DEFINE_TYPE_WITH_PRIVATE (GstVaapiDisplayX11, gst_vaapi_display_x11,
45 GST_TYPE_VAAPI_DISPLAY);
47 static inline const gchar *
48 get_default_display_name (void)
50 static const gchar *g_display_name;
53 g_display_name = getenv ("DISPLAY");
54 return g_display_name;
57 /* Reconstruct a display name without our prefix */
59 get_display_name (GstVaapiDisplayX11 * display)
61 GstVaapiDisplayX11Private *const priv = display->priv;
62 const gchar *display_name = priv->display_name;
64 if (!display_name || *display_name == '\0')
69 /* Mangle display name with our prefix */
71 set_display_name (GstVaapiDisplayX11 * display, const gchar * display_name)
73 GstVaapiDisplayX11Private *const priv = display->priv;
75 g_free (priv->display_name);
78 display_name = get_default_display_name ();
82 priv->display_name = g_strdup (display_name);
83 return priv->display_name != NULL;
86 /* Set synchronous behavious on the underlying X11 display */
88 set_synchronous (GstVaapiDisplayX11 * display, gboolean synchronous)
90 GstVaapiDisplayX11Private *const priv = display->priv;
92 if (priv->synchronous != synchronous) {
93 priv->synchronous = synchronous;
94 if (priv->x11_display) {
95 GST_VAAPI_DISPLAY_LOCK (display);
96 XSynchronize (priv->x11_display, synchronous);
97 GST_VAAPI_DISPLAY_UNLOCK (display);
102 /* Check for display server extensions */
104 check_extensions (GstVaapiDisplayX11 * display)
106 GstVaapiDisplayX11Private *const priv = display->priv;
107 int evt_base, err_base;
110 priv->use_xrandr = XRRQueryExtension (priv->x11_display,
111 &evt_base, &err_base);
116 gst_vaapi_display_x11_bind_display (GstVaapiDisplay * base_display,
117 gpointer native_display)
119 GstVaapiDisplayX11 *const display = GST_VAAPI_DISPLAY_X11_CAST (base_display);
120 GstVaapiDisplayX11Private *const priv = display->priv;
122 priv->x11_display = native_display;
123 priv->x11_screen = DefaultScreen (native_display);
124 priv->use_foreign_display = TRUE;
126 check_extensions (display);
127 if (!set_display_name (display, XDisplayString (priv->x11_display)))
133 gst_vaapi_display_x11_open_display (GstVaapiDisplay * base_display,
136 GstVaapiDisplayX11 *const display = GST_VAAPI_DISPLAY_X11_CAST (base_display);
137 GstVaapiDisplayX11Private *const priv = display->priv;
139 if (!set_display_name (display, name))
142 priv->x11_display = XOpenDisplay (get_display_name (display));
143 if (!priv->x11_display)
145 priv->use_foreign_display = FALSE;
147 priv->x11_screen = DefaultScreen (priv->x11_display);
149 check_extensions (display);
154 gst_vaapi_display_x11_close_display (GstVaapiDisplay * display)
156 GstVaapiDisplayX11Private *const priv =
157 GST_VAAPI_DISPLAY_X11_PRIVATE (display);
159 g_clear_pointer (&priv->pixmap_formats, g_array_unref);
161 if (priv->x11_display) {
162 if (!priv->use_foreign_display)
163 XCloseDisplay (priv->x11_display);
164 priv->x11_display = NULL;
167 g_clear_pointer (&priv->display_name, g_free);
171 gst_vaapi_display_x11_sync (GstVaapiDisplay * display)
173 GstVaapiDisplayX11Private *const priv =
174 GST_VAAPI_DISPLAY_X11_PRIVATE (display);
176 if (priv->x11_display) {
177 GST_VAAPI_DISPLAY_LOCK (display);
178 XSync (priv->x11_display, False);
179 GST_VAAPI_DISPLAY_UNLOCK (display);
184 gst_vaapi_display_x11_flush (GstVaapiDisplay * display)
186 GstVaapiDisplayX11Private *const priv =
187 GST_VAAPI_DISPLAY_X11_PRIVATE (display);
189 if (priv->x11_display) {
190 GST_VAAPI_DISPLAY_LOCK (display);
191 XFlush (priv->x11_display);
192 GST_VAAPI_DISPLAY_UNLOCK (display);
197 gst_vaapi_display_x11_get_display_info (GstVaapiDisplay * display,
198 GstVaapiDisplayInfo * info)
200 GstVaapiDisplayX11Private *const priv =
201 GST_VAAPI_DISPLAY_X11_PRIVATE (display);
203 info->native_display = priv->x11_display;
204 info->display_name = priv->display_name;
205 if (!info->va_display) {
206 info->va_display = vaGetDisplay (priv->x11_display);
207 if (!info->va_display)
214 gst_vaapi_display_x11_get_size (GstVaapiDisplay * display,
215 guint * pwidth, guint * pheight)
217 GstVaapiDisplayX11Private *const priv =
218 GST_VAAPI_DISPLAY_X11_PRIVATE (display);
220 if (!priv->x11_display)
224 *pwidth = DisplayWidth (priv->x11_display, priv->x11_screen);
227 *pheight = DisplayHeight (priv->x11_display, priv->x11_screen);
231 gst_vaapi_display_x11_get_size_mm (GstVaapiDisplay * display,
232 guint * pwidth, guint * pheight)
234 GstVaapiDisplayX11Private *const priv =
235 GST_VAAPI_DISPLAY_X11_PRIVATE (display);
236 guint width_mm, height_mm;
238 if (!priv->x11_display)
241 width_mm = DisplayWidthMM (priv->x11_display, priv->x11_screen);
242 height_mm = DisplayHeightMM (priv->x11_display, priv->x11_screen);
245 /* XXX: fix up physical size if the display is rotated */
246 if (priv->use_xrandr) {
247 XRRScreenConfiguration *xrr_config = NULL;
248 XRRScreenSize *xrr_sizes;
250 int num_xrr_sizes, size_id, screen;
254 win = DefaultRootWindow (priv->x11_display);
255 screen = XRRRootToScreen (priv->x11_display, win);
257 xrr_config = XRRGetScreenInfo (priv->x11_display, win);
261 size_id = XRRConfigCurrentConfiguration (xrr_config, &rotation);
262 if (rotation == RR_Rotate_0 || rotation == RR_Rotate_180)
265 xrr_sizes = XRRSizes (priv->x11_display, screen, &num_xrr_sizes);
266 if (!xrr_sizes || size_id >= num_xrr_sizes)
269 width_mm = xrr_sizes[size_id].mheight;
270 height_mm = xrr_sizes[size_id].mwidth;
273 XRRFreeScreenConfigInfo (xrr_config);
281 *pheight = height_mm;
284 static GstVaapiWindow *
285 gst_vaapi_display_x11_create_window (GstVaapiDisplay * display, GstVaapiID id,
286 guint width, guint height)
288 return id != GST_VAAPI_ID_INVALID ?
289 gst_vaapi_window_x11_new_with_xid (display, id) :
290 gst_vaapi_window_x11_new (display, width, height);
294 gst_vaapi_display_x11_class_init (GstVaapiDisplayX11Class * klass)
296 GstVaapiDisplayClass *const dpy_class = GST_VAAPI_DISPLAY_CLASS (klass);
298 dpy_class->display_type = GST_VAAPI_DISPLAY_TYPE_X11;
299 dpy_class->bind_display = gst_vaapi_display_x11_bind_display;
300 dpy_class->open_display = gst_vaapi_display_x11_open_display;
301 dpy_class->close_display = gst_vaapi_display_x11_close_display;
302 dpy_class->sync = gst_vaapi_display_x11_sync;
303 dpy_class->flush = gst_vaapi_display_x11_flush;
304 dpy_class->get_display = gst_vaapi_display_x11_get_display_info;
305 dpy_class->get_size = gst_vaapi_display_x11_get_size;
306 dpy_class->get_size_mm = gst_vaapi_display_x11_get_size_mm;
307 dpy_class->create_window = gst_vaapi_display_x11_create_window;
311 gst_vaapi_display_x11_init (GstVaapiDisplayX11 * display)
313 GstVaapiDisplayX11Private *const priv =
314 gst_vaapi_display_x11_get_instance_private (display);
316 display->priv = priv;
320 * gst_vaapi_display_x11_new:
321 * @display_name: the X11 display name
323 * Opens an X11 #Display using @display_name and returns a newly
324 * allocated #GstVaapiDisplay object. The X11 display will be cloed
325 * when the reference count of the object reaches zero.
327 * Return value: a newly allocated #GstVaapiDisplay object
330 gst_vaapi_display_x11_new (const gchar * display_name)
332 GstVaapiDisplay *display;
334 display = g_object_new (GST_TYPE_VAAPI_DISPLAY_X11, NULL);
335 return gst_vaapi_display_config (display,
336 GST_VAAPI_DISPLAY_INIT_FROM_DISPLAY_NAME, (gpointer) display_name);
340 * gst_vaapi_display_x11_new_with_display:
341 * @x11_display: an X11 #Display
343 * Creates a #GstVaapiDisplay based on the X11 @x11_display
344 * display. The caller still owns the display and must call
345 * XCloseDisplay() when all #GstVaapiDisplay references are
346 * released. Doing so too early can yield undefined behaviour.
348 * Return value: a newly allocated #GstVaapiDisplay object
351 gst_vaapi_display_x11_new_with_display (Display * x11_display)
353 GstVaapiDisplay *display;
355 g_return_val_if_fail (x11_display, NULL);
357 display = g_object_new (GST_TYPE_VAAPI_DISPLAY_X11, NULL);
358 return gst_vaapi_display_config (display,
359 GST_VAAPI_DISPLAY_INIT_FROM_NATIVE_DISPLAY, x11_display);
363 gst_vaapi_display_x11_new_with_va_display (VADisplay va_display,
364 Display * x11_display)
366 GstVaapiDisplay *display;
367 GstVaapiDisplayInfo info = {
368 .va_display = va_display,
369 .native_display = x11_display,
372 g_return_val_if_fail (x11_display, NULL);
374 display = g_object_new (GST_TYPE_VAAPI_DISPLAY_X11, NULL);
375 return gst_vaapi_display_config (display,
376 GST_VAAPI_DISPLAY_INIT_FROM_VA_DISPLAY, &info);
380 * gst_vaapi_display_x11_get_display:
381 * @display: a #GstVaapiDisplayX11
383 * Returns the underlying X11 #Display that was created by
384 * gst_vaapi_display_x11_new() or that was bound from
385 * gst_vaapi_display_x11_new_with_display().
387 * Return value: the X11 #Display attached to @display
390 gst_vaapi_display_x11_get_display (GstVaapiDisplayX11 * display)
392 g_return_val_if_fail (GST_VAAPI_IS_DISPLAY_X11 (display), NULL);
394 return GST_VAAPI_DISPLAY_XDISPLAY (display);
398 * gst_vaapi_display_x11_get_screen:
399 * @display: a #GstVaapiDisplayX11
401 * Returns the default X11 screen that was created by
402 * gst_vaapi_display_x11_new() or that was bound from
403 * gst_vaapi_display_x11_new_with_display().
405 * Return value: the X11 #Display attached to @display
408 gst_vaapi_display_x11_get_screen (GstVaapiDisplayX11 * display)
410 g_return_val_if_fail (GST_VAAPI_IS_DISPLAY_X11 (display), -1);
412 return GST_VAAPI_DISPLAY_XSCREEN (display);
416 * gst_vaapi_display_x11_set_synchronous:
417 * @display: a #GstVaapiDisplayX11
418 * @synchronous: boolean value that indicates whether to enable or
419 * disable synchronization
421 * If @synchronous is %TRUE, gst_vaapi_display_x11_set_synchronous()
422 * turns on synchronous behaviour on the underlying X11
423 * display. Otherwise, synchronous behaviour is disabled if
424 * @synchronous is %FALSE.
427 gst_vaapi_display_x11_set_synchronous (GstVaapiDisplayX11 * display,
428 gboolean synchronous)
430 g_return_if_fail (GST_VAAPI_IS_DISPLAY_X11 (display));
432 set_synchronous (display, synchronous);
435 typedef struct _GstVaapiPixmapFormatX11 GstVaapiPixmapFormatX11;
436 struct _GstVaapiPixmapFormatX11
438 GstVideoFormat format;
443 static GstVideoFormat
444 pix_fmt_to_video_format (gint depth, gint bpp)
446 GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
451 format = GST_VIDEO_FORMAT_RGB15;
452 else if (depth == 16)
453 format = GST_VIDEO_FORMAT_RGB16;
457 format = GST_VIDEO_FORMAT_RGB;
460 if (depth == 24 || depth == 32)
461 format = GST_VIDEO_FORMAT_xRGB;
468 ensure_pix_fmts (GstVaapiDisplayX11 * display)
470 GstVaapiDisplayX11Private *const priv =
471 GST_VAAPI_DISPLAY_X11_PRIVATE (display);
472 XPixmapFormatValues *pix_fmts;
473 int i, n, num_pix_fmts;
475 if (priv->pixmap_formats)
478 GST_VAAPI_DISPLAY_LOCK (display);
479 pix_fmts = XListPixmapFormats (GST_VAAPI_DISPLAY_XDISPLAY (display),
481 GST_VAAPI_DISPLAY_UNLOCK (display);
485 priv->pixmap_formats = g_array_sized_new (FALSE, FALSE,
486 sizeof (GstVaapiPixmapFormatX11), num_pix_fmts);
487 if (!priv->pixmap_formats) {
492 for (i = 0, n = 0; i < num_pix_fmts; i++) {
493 GstVaapiPixmapFormatX11 *const pix_fmt =
494 &g_array_index (priv->pixmap_formats, GstVaapiPixmapFormatX11, n);
496 pix_fmt->depth = pix_fmts[i].depth;
497 pix_fmt->bpp = pix_fmts[i].bits_per_pixel;
498 pix_fmt->format = pix_fmt_to_video_format (pix_fmt->depth, pix_fmt->bpp);
499 if (pix_fmt->format != GST_VIDEO_FORMAT_UNKNOWN)
502 priv->pixmap_formats->len = n;
507 /* Determine the GstVideoFormat based on a supported Pixmap depth */
509 gst_vaapi_display_x11_get_pixmap_format (GstVaapiDisplayX11 * display,
512 if (ensure_pix_fmts (display)) {
513 GstVaapiDisplayX11Private *const priv =
514 GST_VAAPI_DISPLAY_X11_PRIVATE (display);
517 for (i = 0; i < priv->pixmap_formats->len; i++) {
518 GstVaapiPixmapFormatX11 *const pix_fmt =
519 &g_array_index (priv->pixmap_formats, GstVaapiPixmapFormatX11, i);
520 if (pix_fmt->depth == depth)
521 return pix_fmt->format;
524 return GST_VIDEO_FORMAT_UNKNOWN;
527 /* Determine the Pixmap depth based on a GstVideoFormat */
529 gst_vaapi_display_x11_get_pixmap_depth (GstVaapiDisplayX11 * display,
530 GstVideoFormat format)
532 if (ensure_pix_fmts (display)) {
533 GstVaapiDisplayX11Private *const priv =
534 GST_VAAPI_DISPLAY_X11_PRIVATE (display);
537 for (i = 0; i < priv->pixmap_formats->len; i++) {
538 GstVaapiPixmapFormatX11 *const pix_fmt =
539 &g_array_index (priv->pixmap_formats, GstVaapiPixmapFormatX11, i);
540 if (pix_fmt->format == format)
541 return pix_fmt->depth;