2 * gstvaapidisplay_x11.c - VA/X11 display abstraction
4 * Copyright (C) 2010-2011 Splitted-Desktop Systems
5 * Copyright (C) 2011-2012 Intel Corporation
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either version 2.1
10 * of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301 USA
24 * SECTION:gstvaapidisplay_x11
25 * @short_description: VA/X11 display abstraction
30 #include "gstvaapiutils.h"
31 #include "gstvaapidisplay_priv.h"
32 #include "gstvaapidisplay_x11.h"
33 #include "gstvaapidisplay_x11_priv.h"
36 # include <X11/extensions/Xrandr.h>
40 #include "gstvaapidebug.h"
42 G_DEFINE_TYPE(GstVaapiDisplayX11,
43 gst_vaapi_display_x11,
44 GST_VAAPI_TYPE_DISPLAY);
55 #define NAME_PREFIX "X11:"
56 #define NAME_PREFIX_LENGTH 4
58 static inline gboolean
59 is_display_name(const gchar *display_name)
61 return strncmp(display_name, NAME_PREFIX, NAME_PREFIX_LENGTH) == 0;
64 static inline const gchar *
65 get_default_display_name(void)
67 static const gchar *g_display_name;
70 g_display_name = getenv("DISPLAY");
71 return g_display_name;
75 compare_display_name(gconstpointer a, gconstpointer b, gpointer user_data)
77 const gchar *cached_name = a, *cached_name_end;
78 const gchar *tested_name = b, *tested_name_end;
79 guint cached_name_length, tested_name_length;
81 if (!cached_name || !is_display_name(cached_name))
83 g_return_val_if_fail(tested_name && is_display_name(tested_name), FALSE);
85 cached_name += NAME_PREFIX_LENGTH;
86 cached_name_end = strchr(cached_name, ':');
88 cached_name_length = cached_name_end - cached_name;
90 cached_name_length = strlen(cached_name);
92 tested_name += NAME_PREFIX_LENGTH;
93 tested_name_end = strchr(tested_name, ':');
95 tested_name_length = tested_name_end - tested_name;
97 tested_name_length = strlen(tested_name);
99 if (cached_name_length != tested_name_length)
101 if (strncmp(cached_name, tested_name, cached_name_length) != 0)
104 /* XXX: handle screen number? */
109 gst_vaapi_display_x11_finalize(GObject *object)
111 G_OBJECT_CLASS(gst_vaapi_display_x11_parent_class)->finalize(object);
114 /* Reconstruct a display name without our prefix */
116 get_display_name(gpointer ptr)
118 GstVaapiDisplayX11 * const display = GST_VAAPI_DISPLAY_X11(ptr);
119 const gchar *display_name = display->priv->display_name;
124 if (is_display_name(display_name)) {
125 display_name += NAME_PREFIX_LENGTH;
126 if (*display_name == '\0')
131 /* XXX: this should not happen */
132 g_assert(0 && "display name without prefix");
136 /* Mangle display name with our prefix */
138 set_display_name(GstVaapiDisplayX11 *display, const gchar *display_name)
140 GstVaapiDisplayX11Private * const priv = display->priv;
142 g_free(priv->display_name);
145 display_name = get_default_display_name();
149 priv->display_name = g_strdup_printf("%s%s", NAME_PREFIX, display_name);
153 set_synchronous(GstVaapiDisplayX11 *display, gboolean synchronous)
155 GstVaapiDisplayX11Private * const priv = display->priv;
157 if (priv->synchronous != synchronous) {
158 priv->synchronous = synchronous;
159 if (priv->x11_display)
160 XSynchronize(priv->x11_display, synchronous);
165 gst_vaapi_display_x11_set_property(
172 GstVaapiDisplayX11 * const display = GST_VAAPI_DISPLAY_X11(object);
175 case PROP_SYNCHRONOUS:
176 set_synchronous(display, g_value_get_boolean(value));
178 case PROP_DISPLAY_NAME:
179 set_display_name(display, g_value_get_string(value));
181 case PROP_X11_DISPLAY:
182 display->priv->x11_display = g_value_get_pointer(value);
184 case PROP_X11_SCREEN:
185 display->priv->x11_screen = g_value_get_int(value);
188 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
194 gst_vaapi_display_x11_get_property(
201 GstVaapiDisplayX11 * const display = GST_VAAPI_DISPLAY_X11(object);
204 case PROP_SYNCHRONOUS:
205 g_value_set_boolean(value, display->priv->synchronous);
207 case PROP_DISPLAY_NAME:
208 g_value_set_string(value, get_display_name(display));
210 case PROP_X11_DISPLAY:
211 g_value_set_pointer(value, gst_vaapi_display_x11_get_display(display));
213 case PROP_X11_SCREEN:
214 g_value_set_int(value, gst_vaapi_display_x11_get_screen(display));
217 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
223 gst_vaapi_display_x11_constructed(GObject *object)
225 GstVaapiDisplayX11 * const display = GST_VAAPI_DISPLAY_X11(object);
226 GstVaapiDisplayX11Private * const priv = display->priv;
227 GstVaapiDisplayCache * const cache = gst_vaapi_display_get_cache();
228 const GstVaapiDisplayInfo *info;
229 GObjectClass *parent_class;
231 priv->create_display = priv->x11_display == NULL;
233 /* Don't create X11 display if there is one in the cache already */
234 if (priv->create_display) {
235 info = gst_vaapi_display_cache_lookup_by_name(
238 compare_display_name, NULL
241 priv->x11_display = info->native_display;
242 priv->create_display = FALSE;
246 /* Reset display-name if the user provided his own X11 display */
247 if (!priv->create_display)
248 set_display_name(display, XDisplayString(priv->x11_display));
250 parent_class = G_OBJECT_CLASS(gst_vaapi_display_x11_parent_class);
251 if (parent_class->constructed)
252 parent_class->constructed(object);
256 gst_vaapi_display_x11_open_display(GstVaapiDisplay *display)
258 GstVaapiDisplayX11Private * const priv =
259 GST_VAAPI_DISPLAY_X11(display)->priv;
261 if (priv->create_display) {
262 priv->x11_display = XOpenDisplay(get_display_name(display));
263 if (!priv->x11_display)
265 priv->x11_screen = DefaultScreen(priv->x11_display);
267 if (!priv->x11_display)
270 if (priv->synchronous)
271 XSynchronize(priv->x11_display, True);
275 int evt_base, err_base;
276 priv->use_xrandr = XRRQueryExtension(
277 priv->x11_display, &evt_base, &err_base);
284 gst_vaapi_display_x11_close_display(GstVaapiDisplay *display)
286 GstVaapiDisplayX11Private * const priv =
287 GST_VAAPI_DISPLAY_X11(display)->priv;
289 if (priv->x11_display) {
290 if (priv->create_display)
291 XCloseDisplay(priv->x11_display);
292 priv->x11_display = NULL;
295 if (priv->display_name) {
296 g_free(priv->display_name);
297 priv->display_name = NULL;
302 gst_vaapi_display_x11_sync(GstVaapiDisplay *display)
304 GstVaapiDisplayX11Private * const priv =
305 GST_VAAPI_DISPLAY_X11(display)->priv;
307 if (priv->x11_display) {
308 GST_VAAPI_DISPLAY_LOCK(display);
309 XSync(priv->x11_display, False);
310 GST_VAAPI_DISPLAY_UNLOCK(display);
315 gst_vaapi_display_x11_flush(GstVaapiDisplay *display)
317 GstVaapiDisplayX11Private * const priv =
318 GST_VAAPI_DISPLAY_X11(display)->priv;
320 if (priv->x11_display) {
321 GST_VAAPI_DISPLAY_LOCK(display);
322 XFlush(priv->x11_display);
323 GST_VAAPI_DISPLAY_UNLOCK(display);
328 gst_vaapi_display_x11_get_display_info(
329 GstVaapiDisplay *display,
330 GstVaapiDisplayInfo *info
333 GstVaapiDisplayX11Private * const priv =
334 GST_VAAPI_DISPLAY_X11(display)->priv;
335 GstVaapiDisplayCache *cache;
336 const GstVaapiDisplayInfo *cached_info;
338 /* Return any cached info even if child has its own VA display */
339 cache = gst_vaapi_display_get_cache();
342 cached_info = gst_vaapi_display_cache_lookup_by_native_display(
343 cache, priv->x11_display);
345 *info = *cached_info;
349 /* Otherwise, create VA display if there is none already */
350 info->native_display = priv->x11_display;
351 info->display_name = priv->display_name;
352 if (!info->va_display) {
353 info->va_display = vaGetDisplay(priv->x11_display);
354 if (!info->va_display)
356 info->display_type = GST_VAAPI_DISPLAY_TYPE_X11;
362 gst_vaapi_display_x11_get_size(
363 GstVaapiDisplay *display,
368 GstVaapiDisplayX11Private * const priv =
369 GST_VAAPI_DISPLAY_X11(display)->priv;
371 if (!priv->x11_display)
375 *pwidth = DisplayWidth(priv->x11_display, priv->x11_screen);
378 *pheight = DisplayHeight(priv->x11_display, priv->x11_screen);
382 gst_vaapi_display_x11_get_size_mm(
383 GstVaapiDisplay *display,
388 GstVaapiDisplayX11Private * const priv =
389 GST_VAAPI_DISPLAY_X11(display)->priv;
390 guint width_mm, height_mm;
392 if (!priv->x11_display)
395 width_mm = DisplayWidthMM(priv->x11_display, priv->x11_screen);
396 height_mm = DisplayHeightMM(priv->x11_display, priv->x11_screen);
399 /* XXX: fix up physical size if the display is rotated */
400 if (priv->use_xrandr) {
401 XRRScreenConfiguration *xrr_config = NULL;
402 XRRScreenSize *xrr_sizes;
404 int num_xrr_sizes, size_id, screen;
408 win = DefaultRootWindow(priv->x11_display);
409 screen = XRRRootToScreen(priv->x11_display, win);
411 xrr_config = XRRGetScreenInfo(priv->x11_display, win);
415 size_id = XRRConfigCurrentConfiguration(xrr_config, &rotation);
416 if (rotation == RR_Rotate_0 || rotation == RR_Rotate_180)
419 xrr_sizes = XRRSizes(priv->x11_display, screen, &num_xrr_sizes);
420 if (!xrr_sizes || size_id >= num_xrr_sizes)
423 width_mm = xrr_sizes[size_id].mheight;
424 height_mm = xrr_sizes[size_id].mwidth;
427 XRRFreeScreenConfigInfo(xrr_config);
435 *pheight = height_mm;
439 gst_vaapi_display_x11_class_init(GstVaapiDisplayX11Class *klass)
441 GObjectClass * const object_class = G_OBJECT_CLASS(klass);
442 GstVaapiDisplayClass * const dpy_class = GST_VAAPI_DISPLAY_CLASS(klass);
444 g_type_class_add_private(klass, sizeof(GstVaapiDisplayX11Private));
446 object_class->finalize = gst_vaapi_display_x11_finalize;
447 object_class->set_property = gst_vaapi_display_x11_set_property;
448 object_class->get_property = gst_vaapi_display_x11_get_property;
449 object_class->constructed = gst_vaapi_display_x11_constructed;
451 dpy_class->open_display = gst_vaapi_display_x11_open_display;
452 dpy_class->close_display = gst_vaapi_display_x11_close_display;
453 dpy_class->sync = gst_vaapi_display_x11_sync;
454 dpy_class->flush = gst_vaapi_display_x11_flush;
455 dpy_class->get_display = gst_vaapi_display_x11_get_display_info;
456 dpy_class->get_size = gst_vaapi_display_x11_get_size;
457 dpy_class->get_size_mm = gst_vaapi_display_x11_get_size_mm;
460 * GstVaapiDisplayX11:synchronous:
462 * When enabled, runs the X display in synchronous mode. Note that
463 * this is used only for debugging.
465 g_object_class_install_property
468 g_param_spec_boolean("synchronous",
470 "Toggles X display synchronous mode",
475 * GstVaapiDisplayX11:x11-display:
477 * The X11 #Display that was created by gst_vaapi_display_x11_new()
478 * or that was bound from gst_vaapi_display_x11_new_with_display().
480 g_object_class_install_property
483 g_param_spec_pointer("x11-display",
486 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
489 * GstVaapiDisplayX11:x11-screen:
491 * The X11 screen that was created by gst_vaapi_display_x11_new()
492 * or that was bound from gst_vaapi_display_x11_new_with_display().
494 g_object_class_install_property
497 g_param_spec_int("x11-screen",
501 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
504 * GstVaapiDisplayX11:display-name:
506 * The X11 display name.
508 g_object_class_install_property
511 g_param_spec_string("display-name",
515 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
519 gst_vaapi_display_x11_init(GstVaapiDisplayX11 *display)
521 GstVaapiDisplayX11Private *priv = GST_VAAPI_DISPLAY_X11_GET_PRIVATE(display);
523 display->priv = priv;
524 priv->create_display = TRUE;
525 priv->x11_display = NULL;
526 priv->x11_screen = 0;
527 priv->display_name = NULL;
528 priv->use_xrandr = FALSE;
532 * gst_vaapi_display_x11_new:
533 * @display_name: the X11 display name
535 * Opens an X11 #Display using @display_name and returns a newly
536 * allocated #GstVaapiDisplay object. The X11 display will be cloed
537 * when the reference count of the object reaches zero.
539 * Return value: a newly allocated #GstVaapiDisplay object
542 gst_vaapi_display_x11_new(const gchar *display_name)
544 return g_object_new(GST_VAAPI_TYPE_DISPLAY_X11,
545 "display-name", display_name,
550 * gst_vaapi_display_x11_new_with_display:
551 * @x11_display: an X11 #Display
553 * Creates a #GstVaapiDisplay based on the X11 @x11_display
554 * display. The caller still owns the display and must call
555 * XCloseDisplay() when all #GstVaapiDisplay references are
556 * released. Doing so too early can yield undefined behaviour.
558 * Return value: a newly allocated #GstVaapiDisplay object
561 gst_vaapi_display_x11_new_with_display(Display *x11_display)
563 g_return_val_if_fail(x11_display, NULL);
565 return g_object_new(GST_VAAPI_TYPE_DISPLAY_X11,
566 "x11-display", x11_display,
567 "x11-screen", DefaultScreen(x11_display),
572 * gst_vaapi_display_x11_get_display:
573 * @display: a #GstVaapiDisplayX11
575 * Returns the underlying X11 #Display that was created by
576 * gst_vaapi_display_x11_new() or that was bound from
577 * gst_vaapi_display_x11_new_with_display().
579 * Return value: the X11 #Display attached to @display
582 gst_vaapi_display_x11_get_display(GstVaapiDisplayX11 *display)
584 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY_X11(display), NULL);
586 return display->priv->x11_display;
590 * gst_vaapi_display_x11_get_screen:
591 * @display: a #GstVaapiDisplayX11
593 * Returns the default X11 screen that was created by
594 * gst_vaapi_display_x11_new() or that was bound from
595 * gst_vaapi_display_x11_new_with_display().
597 * Return value: the X11 #Display attached to @display
600 gst_vaapi_display_x11_get_screen(GstVaapiDisplayX11 *display)
602 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY_X11(display), -1);
604 return display->priv->x11_screen;