Simplify initialization of VADisplay.
[platform/upstream/gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapidisplay_x11.c
1 /*
2  *  gstvaapidisplay_x11.c - VA/X11 display abstraction
3  *
4  *  gstreamer-vaapi (C) 2010 Splitted-Desktop Systems
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
19  */
20
21 /**
22  * SECTION:gstvaapidisplay_x11
23  * @short_description: VA/X11 display abstraction
24  */
25
26 #include "config.h"
27 #include "gstvaapiutils.h"
28 #include "gstvaapidisplay_x11.h"
29
30 #define DEBUG 1
31 #include "gstvaapidebug.h"
32
33 G_DEFINE_TYPE(GstVaapiDisplayX11,
34               gst_vaapi_display_x11,
35               GST_VAAPI_TYPE_DISPLAY);
36
37 #define GST_VAAPI_DISPLAY_X11_GET_PRIVATE(obj)                  \
38     (G_TYPE_INSTANCE_GET_PRIVATE((obj),                         \
39                                  GST_VAAPI_TYPE_DISPLAY_X11,    \
40                                  GstVaapiDisplayX11Private))
41
42 struct _GstVaapiDisplayX11Private {
43     gboolean    create_display;
44     gchar      *display_name;
45     Display    *x11_display;
46     int         x11_screen;
47 };
48
49 enum {
50     PROP_0,
51
52     PROP_DISPLAY_NAME,
53     PROP_X11_DISPLAY
54 };
55
56 static void
57 gst_vaapi_display_x11_finalize(GObject *object)
58 {
59     G_OBJECT_CLASS(gst_vaapi_display_x11_parent_class)->finalize(object);
60 }
61
62 static void
63 set_display_name(GstVaapiDisplayX11 *display, const gchar *display_name)
64 {
65     GstVaapiDisplayX11Private * const priv = display->priv;
66
67     g_free(priv->display_name);
68
69     if (display_name)
70         priv->display_name = g_strdup(display_name);
71     else
72         priv->display_name = NULL;
73 }
74
75 static void
76 gst_vaapi_display_x11_set_property(
77     GObject      *object,
78     guint         prop_id,
79     const GValue *value,
80     GParamSpec   *pspec
81 )
82 {
83     GstVaapiDisplayX11 * const display = GST_VAAPI_DISPLAY_X11(object);
84
85     switch (prop_id) {
86     case PROP_DISPLAY_NAME:
87         set_display_name(display, g_value_get_string(value));
88         break;
89     case PROP_X11_DISPLAY:
90         display->priv->x11_display = g_value_get_pointer(value);
91         break;
92     default:
93         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
94         break;
95     }
96 }
97
98 static void
99 gst_vaapi_display_x11_get_property(
100     GObject    *object,
101     guint       prop_id,
102     GValue     *value,
103     GParamSpec *pspec
104 )
105 {
106     GstVaapiDisplayX11 * const display = GST_VAAPI_DISPLAY_X11(object);
107
108     switch (prop_id) {
109     case PROP_DISPLAY_NAME:
110         g_value_set_string(value, display->priv->display_name);
111         break;
112     case PROP_X11_DISPLAY:
113         g_value_set_pointer(value, gst_vaapi_display_x11_get_display(display));
114         break;
115     default:
116         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
117         break;
118     }
119 }
120
121 static void
122 gst_vaapi_display_x11_constructed(GObject *object)
123 {
124     GstVaapiDisplayX11 * const display = GST_VAAPI_DISPLAY_X11(object);
125     GObjectClass *parent_class;
126
127     display->priv->create_display = display->priv->x11_display == NULL;
128
129     /* Reset display-name if the user provided his own X11 display */
130     if (!display->priv->create_display)
131         set_display_name(display, XDisplayString(display->priv->x11_display));
132
133     parent_class = G_OBJECT_CLASS(gst_vaapi_display_x11_parent_class);
134     if (parent_class->constructed)
135         parent_class->constructed(object);
136 }
137
138 static gboolean
139 gst_vaapi_display_x11_open_display(GstVaapiDisplay *display)
140 {
141     GstVaapiDisplayX11Private * const priv =
142         GST_VAAPI_DISPLAY_X11(display)->priv;
143
144     /* XXX: maintain an X11 display cache */
145     if (!priv->x11_display && priv->create_display)
146         priv->x11_display = XOpenDisplay(priv->display_name);
147     if (!priv->x11_display)
148         return FALSE;
149
150     priv->x11_screen = DefaultScreen(priv->x11_display);
151     return TRUE;
152 }
153
154 static void
155 gst_vaapi_display_x11_close_display(GstVaapiDisplay *display)
156 {
157     GstVaapiDisplayX11Private * const priv =
158         GST_VAAPI_DISPLAY_X11(display)->priv;
159
160     if (priv->x11_display) {
161         if (priv->create_display)
162             XCloseDisplay(priv->x11_display);
163         priv->x11_display = NULL;
164     }
165
166     if (priv->display_name) {
167         g_free(priv->display_name);
168         priv->display_name = NULL;
169     }
170 }
171
172 static VADisplay
173 gst_vaapi_display_x11_get_va_display(GstVaapiDisplay *display)
174 {
175     return vaGetDisplay(GST_VAAPI_DISPLAY_XDISPLAY(display));
176 }
177
178 static void
179 gst_vaapi_display_x11_get_size(
180     GstVaapiDisplay *display,
181     guint           *pwidth,
182     guint           *pheight
183 )
184 {
185     GstVaapiDisplayX11Private * const priv =
186         GST_VAAPI_DISPLAY_X11(display)->priv;
187
188     if (!priv->x11_display)
189         return;
190
191     if (pwidth)
192         *pwidth = DisplayWidth(priv->x11_display, priv->x11_screen);
193
194     if (pheight)
195         *pheight = DisplayHeight(priv->x11_display, priv->x11_screen);
196 }
197
198 static void
199 gst_vaapi_display_x11_get_size_mm(
200     GstVaapiDisplay *display,
201     guint           *pwidth,
202     guint           *pheight
203 )
204 {
205     GstVaapiDisplayX11Private * const priv =
206         GST_VAAPI_DISPLAY_X11(display)->priv;
207
208     if (!priv->x11_display)
209         return;
210
211     if (pwidth)
212         *pwidth = DisplayWidthMM(priv->x11_display, priv->x11_screen);
213
214     if (pheight)
215         *pheight = DisplayHeightMM(priv->x11_display, priv->x11_screen);
216 }
217
218 static void
219 gst_vaapi_display_x11_class_init(GstVaapiDisplayX11Class *klass)
220 {
221     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
222     GstVaapiDisplayClass * const dpy_class = GST_VAAPI_DISPLAY_CLASS(klass);
223
224     g_type_class_add_private(klass, sizeof(GstVaapiDisplayX11Private));
225
226     object_class->finalize      = gst_vaapi_display_x11_finalize;
227     object_class->set_property  = gst_vaapi_display_x11_set_property;
228     object_class->get_property  = gst_vaapi_display_x11_get_property;
229     object_class->constructed   = gst_vaapi_display_x11_constructed;
230
231     dpy_class->open_display     = gst_vaapi_display_x11_open_display;
232     dpy_class->close_display    = gst_vaapi_display_x11_close_display;
233     dpy_class->get_display      = gst_vaapi_display_x11_get_va_display;
234     dpy_class->get_size         = gst_vaapi_display_x11_get_size;
235     dpy_class->get_size_mm      = gst_vaapi_display_x11_get_size_mm;
236
237     /**
238      * GstVaapiDisplayX11:x11-display:
239      *
240      * The X11 #Display that was created by gst_vaapi_display_x11_new()
241      * or that was bound from gst_vaapi_display_x11_new_with_display().
242      */
243     g_object_class_install_property
244         (object_class,
245          PROP_X11_DISPLAY,
246          g_param_spec_pointer("x11-display",
247                               "X11 display",
248                               "X11 display",
249                               G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
250
251     /**
252      * GstVaapiDisplayX11:display-name:
253      *
254      * The X11 display name.
255      */
256     g_object_class_install_property
257         (object_class,
258          PROP_DISPLAY_NAME,
259          g_param_spec_string("display-name",
260                              "X11 display name",
261                              "X11 display name",
262                              NULL,
263                              G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
264 }
265
266 static void
267 gst_vaapi_display_x11_init(GstVaapiDisplayX11 *display)
268 {
269     GstVaapiDisplayX11Private *priv = GST_VAAPI_DISPLAY_X11_GET_PRIVATE(display);
270
271     display->priv        = priv;
272     priv->create_display = TRUE;
273     priv->x11_display    = NULL;
274     priv->x11_screen     = 0;
275     priv->display_name   = NULL;
276 }
277
278 /**
279  * gst_vaapi_display_x11_new:
280  * @display_name: the X11 display name
281  *
282  * Opens an X11 #Display using @display_name and returns a newly
283  * allocated #GstVaapiDisplay object. The X11 display will be cloed
284  * when the reference count of the object reaches zero.
285  *
286  * Return value: a newly allocated #GstVaapiDisplay object
287  */
288 GstVaapiDisplay *
289 gst_vaapi_display_x11_new(const gchar *display_name)
290 {
291     return g_object_new(GST_VAAPI_TYPE_DISPLAY_X11,
292                         "display-name", display_name,
293                         NULL);
294 }
295
296 /**
297  * gst_vaapi_display_x11_new_with_display:
298  * @x11_display: an X11 #Display
299  *
300  * Creates a #GstVaapiDisplay based on the X11 @x11_display
301  * display. The caller still owns the display and must call
302  * XCloseDisplay() when all #GstVaapiDisplay references are
303  * released. Doing so too early can yield undefined behaviour.
304  *
305  * Return value: a newly allocated #GstVaapiDisplay object
306  */
307 GstVaapiDisplay *
308 gst_vaapi_display_x11_new_with_display(Display *x11_display)
309 {
310     return g_object_new(GST_VAAPI_TYPE_DISPLAY_X11,
311                         "x11-display", x11_display,
312                         NULL);
313 }
314
315 /**
316  * gst_vaapi_display_x11_get_display:
317  * @display: a #GstVaapiDisplayX11
318  *
319  * Returns the underlying X11 #Display that was created by
320  * gst_vaapi_display_x11_new() or that was bound from
321  * gst_vaapi_display_x11_new_with_display().
322  *
323  * Return value: the X11 #Display attached to @display
324  */
325 Display *
326 gst_vaapi_display_x11_get_display(GstVaapiDisplayX11 *display)
327 {
328     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY_X11(display), NULL);
329
330     return display->priv->x11_display;
331 }