1 /*
2  *  gstvaapidisplay_glx.c - VA/GLX display abstraction
3  *
4  *  Copyright (C) 2010-2011 Splitted-Desktop Systems
5  *    Author: Gwenole Beauchesne <gwenole.beauchesne@splitted-desktop.com>
6  *  Copyright (C) 2011-2013 Intel Corporation
7  *    Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
8  *
9  *  This library is free software; you can redistribute it and/or
10  *  modify it under the terms of the GNU Lesser General Public License
11  *  as published by the Free Software Foundation; either version 2.1
12  *  of the License, or (at your option) any later version.
13  *
14  *  This library is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  Lesser General Public License for more details.
18  *
19  *  You should have received a copy of the GNU Lesser General Public
20  *  License along with this library; if not, write to the Free
21  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  *  Boston, MA 02110-1301 USA
23  */
24 
25 /**
26  * SECTION:gstvaapidisplay_glx
27  * @short_description: VA/GLX display abstraction
28  */
29 
30 #include "sysdeps.h"
31 #include "gstvaapicompat.h"
32 #include "gstvaapiutils.h"
33 #include "gstvaapiutils_glx.h"
34 #include "gstvaapidisplay_priv.h"
35 #include "gstvaapidisplay_x11_priv.h"
36 #include "gstvaapidisplay_glx.h"
37 #include "gstvaapidisplay_glx_priv.h"
38 #include "gstvaapiwindow_glx.h"
39 #include "gstvaapitexture_glx.h"
40 
41 #define DEBUG_VAAPI_DISPLAY 1
42 #include "gstvaapidebug.h"
43 
44 G_DEFINE_TYPE (GstVaapiDisplayGLX, gst_vaapi_display_glx,
45     GST_TYPE_VAAPI_DISPLAY_X11);
46 
47 static GstVaapiWindow *
gst_vaapi_display_glx_create_window(GstVaapiDisplay * display,GstVaapiID id,guint width,guint height)48 gst_vaapi_display_glx_create_window (GstVaapiDisplay * display, GstVaapiID id,
49     guint width, guint height)
50 {
51   return id != GST_VAAPI_ID_INVALID ?
52       gst_vaapi_window_glx_new_with_xid (display, id) :
53       gst_vaapi_window_glx_new (display, width, height);
54 }
55 
56 static void
ensure_texture_map(GstVaapiDisplayGLX * display)57 ensure_texture_map (GstVaapiDisplayGLX * display)
58 {
59   if (!display->texture_map)
60     display->texture_map = gst_vaapi_texture_map_new ();
61 }
62 
63 static GstVaapiTexture *
gst_vaapi_display_glx_create_texture(GstVaapiDisplay * display,GstVaapiID id,guint target,guint format,guint width,guint height)64 gst_vaapi_display_glx_create_texture (GstVaapiDisplay * display, GstVaapiID id,
65     guint target, guint format, guint width, guint height)
66 {
67   GstVaapiTexture *texture;
68   GstVaapiDisplayGLX *dpy = GST_VAAPI_DISPLAY_GLX (display);
69 
70   if (id == GST_VAAPI_ID_INVALID)
71     return gst_vaapi_texture_glx_new (display, target, format, width, height);
72 
73   ensure_texture_map (dpy);
74   if (!(texture = gst_vaapi_texture_map_lookup (dpy->texture_map, id))) {
75     if ((texture =
76             gst_vaapi_texture_glx_new_wrapped (display, id, target, format))) {
77       gst_vaapi_texture_map_add (dpy->texture_map, texture, id);
78     }
79   }
80 
81   return texture;
82 }
83 
84 static GstVaapiTextureMap *
gst_vaapi_display_glx_get_texture_map(GstVaapiDisplay * display)85 gst_vaapi_display_glx_get_texture_map (GstVaapiDisplay * display)
86 {
87   return GST_VAAPI_DISPLAY_GLX (display)->texture_map;
88 }
89 
90 static void
gst_vaapi_display_glx_finalize(GObject * object)91 gst_vaapi_display_glx_finalize (GObject * object)
92 {
93   GstVaapiDisplayGLX *dpy = GST_VAAPI_DISPLAY_GLX (object);
94 
95   if (dpy->texture_map)
96     gst_object_unref (dpy->texture_map);
97   G_OBJECT_CLASS (gst_vaapi_display_glx_parent_class)->finalize (object);
98 }
99 
100 static void
gst_vaapi_display_glx_init(GstVaapiDisplayGLX * display)101 gst_vaapi_display_glx_init (GstVaapiDisplayGLX * display)
102 {
103 }
104 
105 static void
gst_vaapi_display_glx_class_init(GstVaapiDisplayGLXClass * klass)106 gst_vaapi_display_glx_class_init (GstVaapiDisplayGLXClass * klass)
107 {
108   GObjectClass *const object_class = G_OBJECT_CLASS (klass);
109   GstVaapiDisplayClass *const dpy_class = GST_VAAPI_DISPLAY_CLASS (klass);
110 
111   object_class->finalize = gst_vaapi_display_glx_finalize;
112   dpy_class->display_type = GST_VAAPI_DISPLAY_TYPE_GLX;
113   dpy_class->create_window = gst_vaapi_display_glx_create_window;
114   dpy_class->create_texture = gst_vaapi_display_glx_create_texture;
115   dpy_class->get_texture_map = gst_vaapi_display_glx_get_texture_map;
116 }
117 
118 /**
119  * gst_vaapi_display_glx_new:
120  * @display_name: the X11 display name
121  *
122  * Opens an X11 #Display using @display_name and returns a newly
123  * allocated #GstVaapiDisplay object. The X11 display will be cloed
124  * when the reference count of the object reaches zero.
125  *
126  * Return value: a newly allocated #GstVaapiDisplay object
127  */
128 GstVaapiDisplay *
gst_vaapi_display_glx_new(const gchar * display_name)129 gst_vaapi_display_glx_new (const gchar * display_name)
130 {
131   GstVaapiDisplay *display;
132 
133   display = g_object_new (GST_TYPE_VAAPI_DISPLAY_GLX, NULL);
134   return gst_vaapi_display_config (display,
135       GST_VAAPI_DISPLAY_INIT_FROM_DISPLAY_NAME, (gpointer) display_name);
136 }
137 
138 /**
139  * gst_vaapi_display_glx_new_with_display:
140  * @x11_display: an X11 #Display
141  *
142  * Creates a #GstVaapiDisplay based on the X11 @x11_display
143  * display. The caller still owns the display and must call
144  * XCloseDisplay() when all #GstVaapiDisplay references are
145  * released. Doing so too early can yield undefined behaviour.
146  *
147  * Return value: a newly allocated #GstVaapiDisplay object
148  */
149 GstVaapiDisplay *
gst_vaapi_display_glx_new_with_display(Display * x11_display)150 gst_vaapi_display_glx_new_with_display (Display * x11_display)
151 {
152   GstVaapiDisplay *display;
153 
154   g_return_val_if_fail (x11_display != NULL, NULL);
155 
156   display = g_object_new (GST_TYPE_VAAPI_DISPLAY_GLX, NULL);
157   return gst_vaapi_display_config (display,
158       GST_VAAPI_DISPLAY_INIT_FROM_NATIVE_DISPLAY, x11_display);
159 }
160