3 * @brief GLX OpenGL extension module
5 /*****************************************************************************
6 * Copyright © 2010-2012 Rémi Denis-Courmont
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
30 #include <GL/glxext.h>
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_opengl.h>
35 #include <vlc_vout_window.h>
38 typedef struct vlc_gl_sys_t
45 static int MakeCurrent (vlc_gl_t *gl)
47 vlc_gl_sys_t *sys = gl->sys;
49 if (!glXMakeContextCurrent (sys->display, sys->win, sys->win, sys->ctx))
54 static void ReleaseCurrent (vlc_gl_t *gl)
56 vlc_gl_sys_t *sys = gl->sys;
58 glXMakeContextCurrent (sys->display, None, None, NULL);
61 static void SwapBuffers (vlc_gl_t *gl)
63 vlc_gl_sys_t *sys = gl->sys;
65 glXSwapBuffers (sys->display, sys->win);
68 static void *GetSymbol(vlc_gl_t *gl, const char *procname)
71 #ifdef GLX_ARB_get_proc_address
72 return glXGetProcAddressARB ((const GLubyte *)procname);
78 static bool CheckGLX (vlc_object_t *vd, Display *dpy)
83 if (!glXQueryVersion (dpy, &major, &minor))
84 msg_Dbg (vd, "GLX extension not available");
87 msg_Dbg (vd, "GLX extension version %d.%d unknown", major, minor);
90 msg_Dbg (vd, "GLX extension version %d.%d too old", major, minor);
93 msg_Dbg (vd, "using GLX extension version %d.%d", major, minor);
99 static bool CheckGLXext (Display *dpy, unsigned snum, const char *ext)
101 const char *exts = glXQueryExtensionsString (dpy, snum);
102 const size_t extlen = strlen (ext);
106 exts += strspn (exts, " ");
108 size_t len = strcspn (exts, " ");
109 if (len == extlen && !memcmp (exts, ext, extlen))
116 static int Open (vlc_object_t *obj)
118 vlc_gl_t *gl = (vlc_gl_t *)obj;
120 if (gl->surface->type != VOUT_WINDOW_TYPE_XID || !vlc_xlib_init (obj))
123 /* Initialize GLX display */
124 Display *dpy = XOpenDisplay (gl->surface->display.x11);
128 vlc_gl_sys_t *sys = malloc (sizeof (*sys));
129 if (unlikely(sys == NULL))
137 if (!CheckGLX (obj, dpy))
140 /* Determine our pixel format */
141 XWindowAttributes wa;
142 if (!XGetWindowAttributes (dpy, gl->surface->handle.xid, &wa))
145 const int snum = XScreenNumberOfScreen (wa.screen);
146 const VisualID visual = XVisualIDFromVisual (wa.visual);
147 static const int attr[] = {
151 GLX_DOUBLEBUFFER, True,
152 GLX_X_RENDERABLE, True,
153 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
158 GLXFBConfig *confs = glXChooseFBConfig (dpy, snum, attr, &nelem);
161 msg_Err (obj, "cannot choose GLX frame buffer configurations");
167 for (int i = 0; i < nelem && !found; i++)
171 XVisualInfo *vi = glXGetVisualFromFBConfig (dpy, conf);
175 if (vi->visualid == visual)
183 msg_Err (obj, "cannot match GLX frame buffer configuration");
187 /* Create a drawing surface */
188 sys->win = glXCreateWindow (dpy, conf, gl->surface->handle.xid, NULL);
189 if (sys->win == None)
191 msg_Err (obj, "cannot create GLX window");
195 /* Create an OpenGL context */
196 sys->ctx = glXCreateNewContext (dpy, conf, GLX_RGBA_TYPE, NULL, True);
197 if (sys->ctx == NULL)
199 glXDestroyWindow (dpy, sys->win);
200 msg_Err (obj, "cannot create GLX context");
204 /* Initialize OpenGL callbacks */
206 gl->makeCurrent = MakeCurrent;
207 gl->releaseCurrent = ReleaseCurrent;
209 gl->swap = SwapBuffers;
210 gl->getProcAddress = GetSymbol;
212 #ifdef GLX_ARB_get_proc_address
213 bool is_swap_interval_set = false;
216 # ifdef GLX_SGI_swap_control
217 if (!is_swap_interval_set
218 && CheckGLXext (dpy, snum, "GLX_SGI_swap_control"))
220 PFNGLXSWAPINTERVALSGIPROC SwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)
221 glXGetProcAddressARB ((const GLubyte *)"glXSwapIntervalSGI");
222 assert (SwapIntervalSGI != NULL);
223 is_swap_interval_set = !SwapIntervalSGI (1);
226 # ifdef GLX_EXT_swap_control
227 if (!is_swap_interval_set
228 && CheckGLXext (dpy, snum, "GLX_EXT_swap_control"))
230 PFNGLXSWAPINTERVALEXTPROC SwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)
231 glXGetProcAddress ((const GLubyte *)"glXSwapIntervalEXT");
232 assert (SwapIntervalEXT != NULL);
233 SwapIntervalEXT (dpy, sys->win, 1);
234 is_swap_interval_set = true;
248 static void Close (vlc_object_t *obj)
250 vlc_gl_t *gl = (vlc_gl_t *)obj;
251 vlc_gl_sys_t *sys = gl->sys;
252 Display *dpy = sys->display;
254 glXDestroyContext (dpy, sys->ctx);
255 glXDestroyWindow (dpy, sys->win);
261 set_shortname (N_("GLX"))
262 set_description (N_("GLX extension for OpenGL"))
263 set_category (CAT_VIDEO)
264 set_subcategory (SUBCAT_VIDEO_VOUT)
265 set_capability ("opengl", 20)
266 set_callbacks (Open, Close)