Add tedious documentation.
[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:gst-vaapi-display-x11
23  * @short_description:
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     VADisplay  *va_display;
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->va_display = vaGetDisplay(priv->x11_display);
151     return priv->va_display != NULL;
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     priv->va_display = NULL;
172 }
173
174 static VADisplay
175 gst_vaapi_display_x11_get_va_display(GstVaapiDisplay *display)
176 {
177     return GST_VAAPI_DISPLAY_X11(display)->priv->va_display;
178 }
179
180 static void
181 gst_vaapi_display_x11_class_init(GstVaapiDisplayX11Class *klass)
182 {
183     GObjectClass * const object_class = G_OBJECT_CLASS(klass);
184     GstVaapiDisplayClass * const dpy_class = GST_VAAPI_DISPLAY_CLASS(klass);
185
186     g_type_class_add_private(klass, sizeof(GstVaapiDisplayX11Private));
187
188     object_class->finalize      = gst_vaapi_display_x11_finalize;
189     object_class->set_property  = gst_vaapi_display_x11_set_property;
190     object_class->get_property  = gst_vaapi_display_x11_get_property;
191     object_class->constructed   = gst_vaapi_display_x11_constructed;
192
193     dpy_class->open_display     = gst_vaapi_display_x11_open_display;
194     dpy_class->close_display    = gst_vaapi_display_x11_close_display;
195     dpy_class->get_display      = gst_vaapi_display_x11_get_va_display;
196
197     /**
198      * GstVaapiDisplayX11:x11-display:
199      *
200      * The X11 #Display that was created by gst_vaapi_display_x11_new()
201      * or that was bound from gst_vaapi_display_x11_new_with_display().
202      */
203     g_object_class_install_property
204         (object_class,
205          PROP_X11_DISPLAY,
206          g_param_spec_pointer("x11-display",
207                               "X11 display",
208                               "X11 display",
209                               G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
210
211     /**
212      * GstVaapiDisplayX11:display-name:
213      *
214      * The X11 display name.
215      */
216     g_object_class_install_property
217         (object_class,
218          PROP_DISPLAY_NAME,
219          g_param_spec_string("display-name",
220                              "X11 display name",
221                              "X11 display name",
222                              NULL,
223                              G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
224 }
225
226 static void
227 gst_vaapi_display_x11_init(GstVaapiDisplayX11 *display)
228 {
229     GstVaapiDisplayX11Private *priv = GST_VAAPI_DISPLAY_X11_GET_PRIVATE(display);
230
231     display->priv        = priv;
232     priv->create_display = TRUE;
233     priv->x11_display    = NULL;
234     priv->display_name   = NULL;
235 }
236
237 /**
238  * gst_vaapi_display_x11_new:
239  * @display_name: the X11 display name
240  *
241  * Opens an X11 #Display using @display_name and returns a newly
242  * allocated #GstVaapiDisplay object. The X11 display will be cloed
243  * when the reference count of the object reaches zero.
244  *
245  * Return value: a newly allocated #GstVaapiDisplay object
246  */
247 GstVaapiDisplay *
248 gst_vaapi_display_x11_new(const gchar *display_name)
249 {
250     return g_object_new(GST_VAAPI_TYPE_DISPLAY_X11,
251                         "display-name", display_name,
252                         NULL);
253 }
254
255 /**
256  * gst_vaapi_display_x11_new_with_display:
257  * @x11_display: an X11 #Display
258  *
259  * Creates a #GstVaapiDisplay based on the X11 @x11_display
260  * display. The caller still owns the display and must call
261  * XCloseDisplay() when all #GstVaapiDisplay references are
262  * released. Doing so too early can yield undefined behaviour.
263  *
264  * Return value: a newly allocated #GstVaapiDisplay object
265  */
266 GstVaapiDisplay *
267 gst_vaapi_display_x11_new_with_display(Display *x11_display)
268 {
269     return g_object_new(GST_VAAPI_TYPE_DISPLAY_X11,
270                         "x11-display", x11_display,
271                         NULL);
272 }
273
274 /**
275  * gst_vaapi_display_x11_get_display:
276  * @display: a #GstVaapiDisplayX11
277  *
278  * Returns the underlying X11 #Display that was created by
279  * gst_vaapi_display_x11_new() or that was bound from
280  * gst_vaapi_display_x11_new_with_display().
281  *
282  * Return value: the X11 #Display attached to @display
283  */
284 Display *
285 gst_vaapi_display_x11_get_display(GstVaapiDisplayX11 *display)
286 {
287     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY_X11(display), NULL);
288
289     return display->priv->x11_display;
290 }