]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/glx.c
XCB/GLX: resize
[vlc] / modules / video_output / xcb / glx.c
1 /**
2  * @file glx.c
3  * @brief GLX video output module for VLC media player
4  */
5 /*****************************************************************************
6  * Copyright © 2004 the VideoLAN team
7  * Copyright © 2009 Rémi Denis-Courmont
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
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
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  ****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <assert.h>
30
31 #include <xcb/xcb.h>
32 #include <X11/Xlib-xcb.h>
33 #include <GL/glx.h>
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_vout_display.h>
38 #include <vlc_vout_opengl.h>
39 #include "../opengl.h"
40
41 #include "xcb_vlc.h"
42
43 static int  Open (vlc_object_t *);
44 static void Close (vlc_object_t *);
45
46 /*
47  * Module descriptor
48  */
49 vlc_module_begin ()
50     set_shortname (N_("GLX"))
51     set_description (N_("GLX video output (XCB)"))
52     set_category (CAT_VIDEO)
53     set_subcategory (SUBCAT_VIDEO_VOUT)
54     set_capability ("vout display", 20)
55     set_callbacks (Open, Close)
56
57     add_shortcut ("xcb-glx")
58     add_shortcut ("glx")
59 vlc_module_end ()
60
61 struct vout_display_sys_t
62 {
63     Display *display; /* Xlib instance */
64     vout_window_t *embed; /* VLC window (when windowed) */
65
66     xcb_cursor_t cursor; /* blank cursor */
67     xcb_window_t window; /* drawable X window */
68     xcb_window_t glwin; /* GLX window */
69     uint16_t width; /* render pixel width */
70     uint16_t height; /* render pixel height */
71     bool visible; /* whether to draw */
72     bool v1_3; /* whether GLX >= 1.3 is available */
73
74     GLXContext ctx;
75     vout_opengl_t gl;
76     vout_display_opengl_t vgl;
77     picture_pool_t *pool; /* picture pool */
78 };
79
80 static picture_t *Get (vout_display_t *);
81 static void PictureRender (vout_display_t *, picture_t *);
82 static void PictureDisplay (vout_display_t *, picture_t *);
83 static int Control (vout_display_t *, int, va_list);
84 static void Manage (vout_display_t *);
85
86 static void SwapBuffers (vout_opengl_t *gl);
87
88 static vout_window_t *MakeWindow (vout_display_t *vd)
89 {
90     vout_window_cfg_t wnd_cfg;
91
92     memset (&wnd_cfg, 0, sizeof (wnd_cfg));
93     wnd_cfg.type = VOUT_WINDOW_TYPE_XID;
94     wnd_cfg.width  = vd->cfg->display.width;
95     wnd_cfg.height = vd->cfg->display.height;
96
97     vout_window_t *wnd = vout_display_NewWindow (vd, &wnd_cfg);
98     if (wnd == NULL)
99         msg_Err (vd, "parent window not available");
100     return wnd;
101 }
102
103 static const xcb_screen_t *
104 FindWindow (vout_display_t *vd, xcb_connection_t *conn,
105             unsigned *restrict pnum, uint8_t *restrict pdepth,
106             uint16_t *restrict pwidth, uint16_t *restrict pheight)
107 {
108     vout_display_sys_t *sys = vd->sys;
109
110     xcb_get_geometry_reply_t *geo =
111         xcb_get_geometry_reply (conn,
112             xcb_get_geometry (conn, sys->embed->xid), NULL);
113     if (geo == NULL)
114     {
115         msg_Err (vd, "parent window not valid");
116         return NULL;
117     }
118
119     xcb_window_t root = geo->root;
120     *pdepth = geo->depth;
121     *pwidth = geo->width;
122     *pheight = geo->height;
123     free (geo);
124
125     /* Find the selected screen */
126     const xcb_setup_t *setup = xcb_get_setup (conn);
127     const xcb_screen_t *screen = NULL;
128     unsigned num = 0;
129
130     for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
131          i.rem > 0;
132          xcb_screen_next (&i))
133     {
134         if (i.data->root == root)
135         {
136             screen = i.data;
137             break;
138         }
139         num++;
140     }
141
142     if (screen == NULL)
143     {
144         msg_Err (vd, "parent window screen not found");
145         return NULL;
146     }
147     msg_Dbg (vd, "using screen 0x%"PRIx32 " (number: %u)", root, num);
148     *pnum = num;
149     return screen;
150 }
151
152 static bool CheckGLX (vout_display_t *vd, Display *dpy, bool *restrict pv13)
153 {
154     int major, minor;
155     bool ok = false;
156
157     if (!glXQueryVersion (dpy, &major, &minor))
158         msg_Dbg (vd, "GLX extension not available");
159     else
160     if (major != 1)
161         msg_Dbg (vd, "GLX extension version %d.%d unknown", major, minor);
162     else
163     if (minor < 2)
164         msg_Dbg (vd, "GLX extension version %d.%d too old", major, minor);
165     else
166     {
167         msg_Dbg (vd, "using GLX extension version %d.%d", major, minor);
168         ok = true;
169         *pv13 = minor >= 3;
170     }
171     return ok;
172 }
173
174 static int CreateWindow (vout_display_t *vd, xcb_connection_t *conn,
175                          uint_fast8_t depth, xcb_visualid_t vid)
176 {
177     vout_display_sys_t *sys = vd->sys;
178     const uint32_t mask = XCB_CW_EVENT_MASK;
179     const uint32_t values[] = {
180         /* XCB_CW_EVENT_MASK */
181         XCB_EVENT_MASK_VISIBILITY_CHANGE,
182     };
183     xcb_void_cookie_t cc, cm;
184
185     cc = xcb_create_window_checked (conn, depth, sys->window,
186                                     sys->embed->xid, 0, 0,
187                                     sys->width, sys->height, 0,
188                                     XCB_WINDOW_CLASS_INPUT_OUTPUT,
189                                     vid, mask, values);
190     cm = xcb_map_window_checked (conn, sys->window);
191     if (CheckError (vd, conn, "cannot create X11 window", cc)
192      || CheckError (vd, conn, "cannot map X11 window", cm))
193         return VLC_EGENERIC;
194
195     msg_Dbg (vd, "using X11 window %08"PRIx32, sys->window);
196     return VLC_SUCCESS;
197 }
198
199 /**
200  * Probe the X server.
201  */
202 static int Open (vlc_object_t *obj)
203 {
204     vout_display_t *vd = (vout_display_t *)obj;
205     vout_display_sys_t *sys = malloc (sizeof (*sys));
206
207     if (sys == NULL)
208         return VLC_ENOMEM;
209
210     vd->sys = sys;
211     sys->pool = NULL;
212     sys->gl.sys = NULL;
213
214     /* Get window */
215     sys->embed = MakeWindow (vd);
216     if (sys->embed == NULL)
217     {
218         free (sys);
219         return VLC_EGENERIC;
220     }
221
222     /* Connect to X server */
223     Display *dpy = XOpenDisplay (sys->embed->x11_display);
224     if (dpy == NULL)
225     {
226         vout_display_DeleteWindow (vd, sys->embed);
227         free (sys);
228         return VLC_EGENERIC;
229     }
230     sys->display = dpy;
231     sys->ctx = NULL;
232     XSetEventQueueOwner (dpy, XCBOwnsEventQueue);
233
234     if (!CheckGLX (vd, dpy, &sys->v1_3))
235         goto error;
236
237     xcb_connection_t *conn = XGetXCBConnection (dpy);
238     assert (conn);
239     RegisterMouseEvents (obj, conn, sys->embed->xid);
240
241     /* Find window parameters */
242     unsigned snum;
243     uint8_t depth;
244     const xcb_screen_t *scr = FindWindow (vd, conn, &snum, &depth,
245                                           &sys->width, &sys->height);
246     if (scr == NULL)
247         goto error;
248
249     sys->window = xcb_generate_id (conn);
250
251     /* Determine our pixel format */
252     if (sys->v1_3)
253     {   /* GLX 1.3 */
254         static const int attr[] = {
255             GLX_RED_SIZE, 5,
256             GLX_GREEN_SIZE, 5,
257             GLX_BLUE_SIZE, 5,
258             GLX_DOUBLEBUFFER, True,
259             GLX_X_RENDERABLE, True,
260             GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
261             None };
262
263         int nelem;
264         GLXFBConfig *confs = glXChooseFBConfig (dpy, snum, attr, &nelem);
265         if (confs == NULL)
266         {
267             msg_Err (vd, "no GLX frame bufer configurations");
268             goto error;
269         }
270
271         /*XVisualInfo *vi = glXGetVisualFromFBConfig (dpy, confs[0]);*/
272         CreateWindow (vd, conn, depth, 0 /* ??? */);
273         /*XFree (vi);*/
274
275         sys->glwin = glXCreateWindow (dpy, confs[0], sys->window, NULL );
276         if (sys->glwin == None)
277         {
278             msg_Err (vd, "cannot create GLX window");
279             XFree (confs);
280             goto error;
281         }
282
283         /* Create an OpenGL context */
284         sys->ctx = glXCreateNewContext (dpy, confs[0], GLX_RGBA_TYPE, NULL,
285                                         True);
286         XFree (confs);
287         if (sys->ctx == NULL)
288         {
289             msg_Err (vd, "cannot create GLX context");
290             goto error;
291         }
292
293         if (glXMakeContextCurrent (dpy, sys->glwin, sys->glwin, sys->ctx))
294             goto error;
295     }
296     else
297     {   /* GLX 1.2 */
298         int attr[] = {
299             GLX_RGBA,
300             GLX_RED_SIZE, 5,
301             GLX_GREEN_SIZE, 5,
302             GLX_BLUE_SIZE, 5,
303             GLX_DOUBLEBUFFER,
304             None };
305
306         XVisualInfo *vi = glXChooseVisual (dpy, snum, attr);
307         if (vi == NULL)
308         {
309             msg_Err (vd, "cannot find GLX 1.2 visual" );
310             goto error;
311         }
312         msg_Dbg (vd, "using GLX visual ID 0x%"PRIx32, (uint32_t)vi->visualid);
313
314         if (CreateWindow (vd, conn, depth, 0 /* ??? */) == 0)
315             sys->ctx = glXCreateContext (dpy, vi, 0, True);
316         XFree (vi);
317         if (sys->ctx == NULL)
318         {
319             msg_Err (vd, "cannot create GLX context");
320             goto error;
321         }
322
323         if (glXMakeCurrent (dpy, sys->window, sys->ctx) == False)
324             goto error;
325         sys->glwin = sys->window;
326     }
327
328     /* Initialize common OpenGL video display */
329     sys->gl.lock = NULL;
330     sys->gl.unlock = NULL;
331     sys->gl.swap = SwapBuffers;
332     sys->gl.sys = sys;
333
334     if (vout_display_opengl_Init (&sys->vgl, &vd->fmt, &sys->gl))
335     {
336         sys->gl.sys = NULL;
337         goto error;
338     }
339
340     sys->cursor = CreateBlankCursor (conn, scr);
341     sys->visible = false;
342
343     /* */
344     vout_display_info_t info = vd->info;
345     info.has_pictures_invalid = false;
346
347     /* Setup vout_display_t once everything is fine */
348     vd->info = info;
349
350     vd->get = Get;
351     vd->prepare = PictureRender;
352     vd->display = PictureDisplay;
353     vd->control = Control;
354     vd->manage = Manage;
355
356     /* */
357     vout_display_SendEventFullscreen (vd, false);
358     vout_display_SendEventDisplaySize (vd, sys->width, sys->height, false);
359
360     return VLC_SUCCESS;
361
362 error:
363     Close (obj);
364     return VLC_EGENERIC;
365 }
366
367
368 /**
369  * Disconnect from the X server.
370  */
371 static void Close (vlc_object_t *obj)
372 {
373     vout_display_t *vd = (vout_display_t *)obj;
374     vout_display_sys_t *sys = vd->sys;
375     Display *dpy = sys->display;
376
377     if (sys->gl.sys != NULL)
378         vout_display_opengl_Clean (&sys->vgl);
379
380     if (sys->ctx != NULL)
381     {
382         if (sys->v1_3)
383         {
384             glXMakeContextCurrent (dpy, None, None, NULL);
385             glXDestroyWindow (dpy, sys->glwin);
386         }
387         else
388             glXMakeCurrent (dpy, None, NULL);
389         glXDestroyContext (dpy, sys->ctx);
390     }
391     XCloseDisplay (dpy);
392     vout_display_DeleteWindow (vd, sys->embed);
393     free (sys);
394 }
395
396 static void SwapBuffers (vout_opengl_t *gl)
397 {
398     vout_display_sys_t *sys = gl->sys;
399
400     glViewport (0, 0, sys->width, sys->height);
401     glXSwapBuffers (sys->display, sys->glwin);
402 }
403
404 /**
405  * Return a direct buffer
406  */
407 static picture_t *Get (vout_display_t *vd)
408 {
409     vout_display_sys_t *sys = vd->sys;
410
411     if (!sys->pool)
412     {
413         sys->pool = vout_display_opengl_GetPool (&sys->vgl);
414         if (!sys->pool)
415             return NULL;
416     }
417     return picture_pool_Get (sys->pool);
418 }
419
420 static void PictureRender (vout_display_t *vd, picture_t *pic)
421 {
422    vout_display_sys_t *sys = vd->sys;
423
424     vout_display_opengl_Prepare (&sys->vgl, pic);
425 }
426
427 static void PictureDisplay (vout_display_t *vd, picture_t *pic)
428 {
429     vout_display_sys_t *sys = vd->sys;
430
431     vout_display_opengl_Display (&sys->vgl, &vd->source);
432     picture_Release (pic);
433 }
434
435 static int Control (vout_display_t *vd, int query, va_list ap)
436 {
437     vout_display_sys_t *sys = vd->sys;
438
439     switch (query)
440     {
441     case VOUT_DISPLAY_CHANGE_FULLSCREEN:
442     {
443         const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
444         return vout_window_SetFullScreen (sys->embed, c->is_fullscreen);
445     }
446
447     case VOUT_DISPLAY_CHANGE_ON_TOP:
448     {
449         int b_on_top = (int)va_arg (ap, int);
450         return vout_window_SetOnTop (sys->embed, b_on_top);
451     }
452
453     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
454     case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
455     case VOUT_DISPLAY_CHANGE_ZOOM:
456     case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
457     case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
458     {
459         xcb_connection_t *conn = XGetXCBConnection (sys->display);
460         const vout_display_cfg_t *cfg;
461         const video_format_t *source;
462         bool is_forced = false;
463
464         if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
465          || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
466         {
467             source = (const video_format_t *)va_arg (ap, const video_format_t *);
468             cfg = vd->cfg;
469         }
470         else
471         {
472             source = &vd->source;
473             cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
474             if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
475                 is_forced = (bool)va_arg (ap, int);
476         }
477
478         /* */
479         if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
480          && is_forced
481          && (cfg->display.width  != vd->cfg->display.width
482            ||cfg->display.height != vd->cfg->display.height)
483          && vout_window_SetSize (sys->embed,
484                                  cfg->display.width, cfg->display.height))
485             return VLC_EGENERIC;
486
487         vout_display_place_t place;
488         vout_display_PlacePicture (&place, source, cfg, false);
489         sys->width  = place.width;
490         sys->height = place.height;
491
492         /* Move the picture within the window */
493         const uint32_t values[] = { place.x, place.y,
494                                     place.width, place.height, };
495         xcb_configure_window (conn, sys->window,
496                               XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
497                             | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
498                               values);
499         xcb_flush (conn);
500         return VLC_SUCCESS;
501     }
502
503     /* Hide the mouse. It will be send when
504      * vout_display_t::info.b_hide_mouse is false */
505     case VOUT_DISPLAY_HIDE_MOUSE:
506         xcb_change_window_attributes (XGetXCBConnection (sys->display),
507                                       sys->embed->xid,
508                                     XCB_CW_CURSOR, &(uint32_t){ sys->cursor });
509         return VLC_SUCCESS;
510     case VOUT_DISPLAY_RESET_PICTURES:
511         assert (0);
512     default:
513         msg_Err (vd, "Unknown request in XCB vout display");
514         return VLC_EGENERIC;
515     }
516 }
517
518 static void Manage (vout_display_t *vd)
519 {
520     vout_display_sys_t *sys = vd->sys;
521     xcb_connection_t *conn = XGetXCBConnection (sys->display);
522
523     ManageEvent (vd, conn, &sys->visible);
524 }