2 * gstvaapipixmap_x11.c - X11 pixmap abstraction
4 * Copyright (C) 2013 Intel Corporation
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.
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.
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
23 * SECTION:gstvaapipixmap_x11
24 * @short_description: X11 pixmap abstraction
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"
38 #include "gstvaapidebug.h"
40 typedef struct _GstVaapiPixmapX11Class GstVaapiPixmapX11Class;
42 struct _GstVaapiPixmapX11 {
43 GstVaapiPixmap parent_instance;
46 struct _GstVaapiPixmapX11Class {
47 GstVaapiPixmapClass parent_class;
51 gst_vaapi_pixmap_x11_create_from_xid(GstVaapiPixmap *pixmap, Pixmap xid)
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);
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)
74 gst_vaapi_pixmap_x11_create(GstVaapiPixmap *pixmap)
76 GstVaapiDisplayX11 * const display =
77 GST_VAAPI_DISPLAY_X11(GST_VAAPI_OBJECT_DISPLAY(pixmap));
78 Display * const dpy = GST_VAAPI_DISPLAY_XDISPLAY(display);
83 if (pixmap->use_foreign_pixmap)
84 return gst_vaapi_pixmap_x11_create_from_xid(pixmap,
85 GST_VAAPI_OBJECT_ID(pixmap));
87 depth = gst_vaapi_display_x11_get_pixmap_depth(display, pixmap->format);
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);
96 GST_DEBUG("xid %" GST_VAAPI_ID_FORMAT, GST_VAAPI_ID_ARGS(xid));
97 GST_VAAPI_OBJECT_ID(pixmap) = xid;
102 gst_vaapi_pixmap_x11_destroy(GstVaapiPixmap *pixmap)
104 Display * const dpy = GST_VAAPI_OBJECT_XDISPLAY(pixmap);
105 const Pixmap xid = GST_VAAPI_OBJECT_ID(pixmap);
108 if (!pixmap->use_foreign_pixmap) {
109 GST_VAAPI_OBJECT_LOCK_DISPLAY(pixmap);
110 XFreePixmap(dpy, xid);
111 GST_VAAPI_OBJECT_UNLOCK_DISPLAY(pixmap);
113 GST_VAAPI_OBJECT_ID(pixmap) = None;
118 gst_vaapi_pixmap_x11_render(GstVaapiPixmap *pixmap, GstVaapiSurface *surface,
119 const GstVaapiRectangle *crop_rect, guint flags)
121 VASurfaceID surface_id;
124 surface_id = GST_VAAPI_OBJECT_ID(surface);
125 if (surface_id == VA_INVALID_ID)
128 GST_VAAPI_OBJECT_LOCK_DISPLAY(pixmap);
129 status = vaPutSurface(
130 GST_VAAPI_OBJECT_VADISPLAY(pixmap),
132 GST_VAAPI_OBJECT_ID(pixmap),
133 crop_rect->x, crop_rect->y,
134 crop_rect->width, crop_rect->height,
136 GST_VAAPI_PIXMAP_WIDTH(pixmap),
137 GST_VAAPI_PIXMAP_HEIGHT(pixmap),
139 from_GstVaapiSurfaceRenderFlags(flags)
141 GST_VAAPI_OBJECT_UNLOCK_DISPLAY(pixmap);
142 if (!vaapi_check_status(status, "vaPutSurface() [pixmap]"))
148 gst_vaapi_pixmap_x11_class_init(GstVaapiPixmapX11Class *klass)
150 GstVaapiObjectClass * const object_class =
151 GST_VAAPI_OBJECT_CLASS(klass);
152 GstVaapiPixmapClass * const pixmap_class =
153 GST_VAAPI_PIXMAP_CLASS(klass);
155 object_class->finalize = (GstVaapiObjectFinalizeFunc)
156 gst_vaapi_pixmap_x11_destroy;
158 pixmap_class->create = gst_vaapi_pixmap_x11_create;
159 pixmap_class->render = gst_vaapi_pixmap_x11_render;
162 #define gst_vaapi_pixmap_x11_finalize \
163 gst_vaapi_pixmap_x11_destroy
165 GST_VAAPI_OBJECT_DEFINE_CLASS_WITH_CODE(
167 gst_vaapi_pixmap_x11,
168 gst_vaapi_pixmap_x11_class_init(&g_class))
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
177 * Creates a pixmap with the specified @format, @width and
178 * @height. The pixmap will be attached to the @display.
180 * Return value: the newly allocated #GstVaapiPixmap object
183 gst_vaapi_pixmap_x11_new(GstVaapiDisplay *display, GstVideoFormat format,
184 guint width, guint height)
186 GST_DEBUG("new pixmap, format %s, size %ux%u",
187 gst_vaapi_video_format_to_string(format), width, height);
189 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY_X11(display), NULL);
191 return gst_vaapi_pixmap_new(GST_VAAPI_PIXMAP_CLASS(
192 gst_vaapi_pixmap_x11_class()), display, format, width, height);
196 * gst_vaapi_pixmap_x11_new_with_xid:
197 * @display: a #GstVaapiDisplay
198 * @xid: an X11 #Pixmap id
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.
205 * Return value: the newly allocated #GstVaapiPixmap object
208 gst_vaapi_pixmap_x11_new_with_xid(GstVaapiDisplay *display, Pixmap xid)
210 GST_DEBUG("new pixmap from xid 0x%08x", xid);
212 g_return_val_if_fail(GST_VAAPI_IS_DISPLAY_X11(display), NULL);
213 g_return_val_if_fail(xid != None, NULL);
215 return gst_vaapi_pixmap_new_from_native(GST_VAAPI_PIXMAP_CLASS(
216 gst_vaapi_pixmap_x11_class()), display, GSIZE_TO_POINTER(xid));
220 * gst_vaapi_pixmap_x11_get_xid:
221 * @pixmap: a #GstVaapiPixmapX11
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().
227 * Return value: the underlying X11 Pixmap bound to @pixmap.
230 gst_vaapi_pixmap_x11_get_xid(GstVaapiPixmapX11 *pixmap)
232 g_return_val_if_fail(pixmap != NULL, None);
234 return GST_VAAPI_OBJECT_ID(pixmap);
238 * gst_vaapi_pixmap_x11_is_foreign_xid:
239 * @pixmap: a #GstVaapiPixmapX11
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().
244 * Return value: %TRUE if the underlying X pixmap is owned by the
245 * caller (foreign pixmap)
248 gst_vaapi_pixmap_x11_is_foreign_xid(GstVaapiPixmapX11 *pixmap)
250 g_return_val_if_fail(pixmap != NULL, FALSE);
252 return GST_VAAPI_PIXMAP(pixmap)->use_foreign_pixmap;