x11: implement pixmap API.
[platform/upstream/gstreamer-vaapi.git] / gst-libs / gst / vaapi / gstvaapipixmap_x11.c
1 /*
2  *  gstvaapipixmap_x11.c - X11 pixmap abstraction
3  *
4  *  Copyright (C) 2013 Intel Corporation
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public License
8  *  as published by the Free Software Foundation; either version 2.1
9  *  of the License, or (at your option) any later version.
10  *
11  *  This library 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 GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free
18  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  *  Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * SECTION:gstvaapipixmap_x11
24  * @short_description: X11 pixmap abstraction
25  */
26
27 #include "sysdeps.h"
28 #include "gstvaapicompat.h"
29 #include "gstvaapipixmap_x11.h"
30 #include "gstvaapipixmap_priv.h"
31 #include "gstvaapidisplay_x11.h"
32 #include "gstvaapidisplay_x11_priv.h"
33 #include "gstvaapiutils.h"
34 #include "gstvaapiutils_x11.h"
35 #include "gstvaapisurface_priv.h"
36
37 #define DEBUG 1
38 #include "gstvaapidebug.h"
39
40 typedef struct _GstVaapiPixmapX11Class          GstVaapiPixmapX11Class;
41
42 struct _GstVaapiPixmapX11 {
43     GstVaapiPixmap      parent_instance;
44 };
45
46 struct _GstVaapiPixmapX11Class {
47     GstVaapiPixmapClass parent_class;
48 };
49
50 static gboolean
51 gst_vaapi_pixmap_x11_create_from_xid(GstVaapiPixmap *pixmap, Pixmap xid)
52 {
53     guint depth;
54     gboolean success;
55
56     if (!xid)
57         return FALSE;
58
59     GST_VAAPI_OBJECT_LOCK_DISPLAY(pixmap);
60     success = x11_get_geometry(GST_VAAPI_OBJECT_XDISPLAY(pixmap), xid,
61         NULL, NULL, &pixmap->width, &pixmap->height, &depth);
62     GST_VAAPI_OBJECT_UNLOCK_DISPLAY(pixmap);
63     if (!success)
64         return FALSE;
65
66     pixmap->format = gst_vaapi_display_x11_get_pixmap_format(
67         GST_VAAPI_DISPLAY_X11(GST_VAAPI_OBJECT_DISPLAY(pixmap)), depth);
68     if (pixmap->format == GST_VIDEO_FORMAT_UNKNOWN)
69         return FALSE;
70     return TRUE;
71 }
72
73 static gboolean
74 gst_vaapi_pixmap_x11_create(GstVaapiPixmap *pixmap)
75 {
76     GstVaapiDisplayX11 * const display =
77         GST_VAAPI_DISPLAY_X11(GST_VAAPI_OBJECT_DISPLAY(pixmap));
78     Display * const dpy = GST_VAAPI_DISPLAY_XDISPLAY(display);
79     Window rootwin;
80     Pixmap xid;
81     guint depth;
82
83     if (pixmap->use_foreign_pixmap)
84         return gst_vaapi_pixmap_x11_create_from_xid(pixmap,
85             GST_VAAPI_OBJECT_ID(pixmap));
86
87     depth = gst_vaapi_display_x11_get_pixmap_depth(display, pixmap->format);
88     if (!depth)
89         return FALSE;
90
91     GST_VAAPI_OBJECT_LOCK_DISPLAY(pixmap);
92     rootwin = RootWindow(dpy, GST_VAAPI_DISPLAY_XSCREEN(display));
93     xid = XCreatePixmap(dpy, rootwin, pixmap->width, pixmap->height, depth);
94     GST_VAAPI_OBJECT_UNLOCK_DISPLAY(pixmap);
95
96     GST_DEBUG("xid %" GST_VAAPI_ID_FORMAT, GST_VAAPI_ID_ARGS(xid));
97     GST_VAAPI_OBJECT_ID(pixmap) = xid;
98     return xid != None;
99 }
100
101 static void
102 gst_vaapi_pixmap_x11_destroy(GstVaapiPixmap *pixmap)
103 {
104     Display * const dpy = GST_VAAPI_OBJECT_XDISPLAY(pixmap);
105     const Pixmap xid = GST_VAAPI_OBJECT_ID(pixmap);
106
107     if (xid) {
108         if (!pixmap->use_foreign_pixmap) {
109             GST_VAAPI_OBJECT_LOCK_DISPLAY(pixmap);
110             XFreePixmap(dpy, xid);
111             GST_VAAPI_OBJECT_UNLOCK_DISPLAY(pixmap);
112         }
113         GST_VAAPI_OBJECT_ID(pixmap) = None;
114     }
115 }
116
117 static gboolean
118 gst_vaapi_pixmap_x11_render(GstVaapiPixmap *pixmap, GstVaapiSurface *surface,
119     const GstVaapiRectangle *crop_rect, guint flags)
120 {
121     VASurfaceID surface_id;
122     VAStatus status;
123
124     surface_id = GST_VAAPI_OBJECT_ID(surface);
125     if (surface_id == VA_INVALID_ID)
126         return FALSE;
127
128     GST_VAAPI_OBJECT_LOCK_DISPLAY(pixmap);
129     status = vaPutSurface(
130         GST_VAAPI_OBJECT_VADISPLAY(pixmap),
131         surface_id,
132         GST_VAAPI_OBJECT_ID(pixmap),
133         crop_rect->x, crop_rect->y,
134         crop_rect->width, crop_rect->height,
135         0, 0,
136         GST_VAAPI_PIXMAP_WIDTH(pixmap),
137         GST_VAAPI_PIXMAP_HEIGHT(pixmap),
138         NULL, 0,
139         from_GstVaapiSurfaceRenderFlags(flags)
140     );
141     GST_VAAPI_OBJECT_UNLOCK_DISPLAY(pixmap);
142     if (!vaapi_check_status(status, "vaPutSurface() [pixmap]"))
143         return FALSE;
144     return TRUE;
145 }
146
147 void
148 gst_vaapi_pixmap_x11_class_init(GstVaapiPixmapX11Class *klass)
149 {
150     GstVaapiObjectClass * const object_class =
151         GST_VAAPI_OBJECT_CLASS(klass);
152     GstVaapiPixmapClass * const pixmap_class =
153         GST_VAAPI_PIXMAP_CLASS(klass);
154
155     object_class->finalize = (GstVaapiObjectFinalizeFunc)
156         gst_vaapi_pixmap_x11_destroy;
157
158     pixmap_class->create        = gst_vaapi_pixmap_x11_create;
159     pixmap_class->render        = gst_vaapi_pixmap_x11_render;
160 }
161
162 #define gst_vaapi_pixmap_x11_finalize \
163     gst_vaapi_pixmap_x11_destroy
164
165 GST_VAAPI_OBJECT_DEFINE_CLASS_WITH_CODE(
166     GstVaapiPixmapX11,
167     gst_vaapi_pixmap_x11,
168     gst_vaapi_pixmap_x11_class_init(&g_class))
169
170 /**
171  * gst_vaapi_pixmap_x11_new:
172  * @display: a #GstVaapiDisplay
173  * @format: the requested pixmap format
174  * @width: the requested pixmap width, in pixels
175  * @height: the requested windo height, in pixels
176  *
177  * Creates a pixmap with the specified @format, @width and
178  * @height. The pixmap will be attached to the @display.
179  *
180  * Return value: the newly allocated #GstVaapiPixmap object
181  */
182 GstVaapiPixmap *
183 gst_vaapi_pixmap_x11_new(GstVaapiDisplay *display, GstVideoFormat format,
184     guint width, guint height)
185 {
186     GST_DEBUG("new pixmap, format %s, size %ux%u",
187               gst_vaapi_video_format_to_string(format), width, height);
188
189     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY_X11(display), NULL);
190
191     return gst_vaapi_pixmap_new(GST_VAAPI_PIXMAP_CLASS(
192             gst_vaapi_pixmap_x11_class()), display, format, width, height);
193 }
194
195 /**
196  * gst_vaapi_pixmap_x11_new_with_xid:
197  * @display: a #GstVaapiDisplay
198  * @xid: an X11 #Pixmap id
199  *
200  * Creates a #GstVaapiPixmap using the X11 Pixmap @xid. The caller
201  * still owns the pixmap and must call XFreePixmap() when all
202  * #GstVaapiPixmap references are released. Doing so too early can
203  * yield undefined behaviour.
204  *
205  * Return value: the newly allocated #GstVaapiPixmap object
206  */
207 GstVaapiPixmap *
208 gst_vaapi_pixmap_x11_new_with_xid(GstVaapiDisplay *display, Pixmap xid)
209 {
210     GST_DEBUG("new pixmap from xid 0x%08x", xid);
211
212     g_return_val_if_fail(GST_VAAPI_IS_DISPLAY_X11(display), NULL);
213     g_return_val_if_fail(xid != None, NULL);
214
215     return gst_vaapi_pixmap_new_from_native(GST_VAAPI_PIXMAP_CLASS(
216             gst_vaapi_pixmap_x11_class()), display, GSIZE_TO_POINTER(xid));
217 }
218
219 /**
220  * gst_vaapi_pixmap_x11_get_xid:
221  * @pixmap: a #GstVaapiPixmapX11
222  *
223  * Returns the underlying X11 Pixmap that was created by
224  * gst_vaapi_pixmap_x11_new() or that was bound with
225  * gst_vaapi_pixmap_x11_new_with_xid().
226  *
227  * Return value: the underlying X11 Pixmap bound to @pixmap.
228  */
229 Pixmap
230 gst_vaapi_pixmap_x11_get_xid(GstVaapiPixmapX11 *pixmap)
231 {
232     g_return_val_if_fail(pixmap != NULL, None);
233
234     return GST_VAAPI_OBJECT_ID(pixmap);
235 }
236
237 /**
238  * gst_vaapi_pixmap_x11_is_foreign_xid:
239  * @pixmap: a #GstVaapiPixmapX11
240  *
241  * Checks whether the @pixmap XID was created by gst_vaapi_pixmap_x11_new()
242  * or was bound with gst_vaapi_pixmap_x11_new_with_xid().
243  *
244  * Return value: %TRUE if the underlying X pixmap is owned by the
245  *   caller (foreign pixmap)
246  */
247 gboolean
248 gst_vaapi_pixmap_x11_is_foreign_xid(GstVaapiPixmapX11 *pixmap)
249 {
250     g_return_val_if_fail(pixmap != NULL, FALSE);
251
252     return GST_VAAPI_PIXMAP(pixmap)->use_foreign_pixmap;
253 }