]> git.sesse.net Git - vlc/blob - modules/video_output/gl.c
Don't show 'Open Graphic Library' to users
[vlc] / modules / video_output / gl.c
1 /**
2  * @file gl.c
3  * @brief OpenGL video output module
4  */
5 /*****************************************************************************
6  * Copyright © 2010-2011 Rémi Denis-Courmont
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1
11  * of the License, or (at your option) any later version.
12  *
13  * This library 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 General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  ****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <assert.h>
29
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_vout_display.h>
33 #include <vlc_opengl.h>
34 #include "opengl.h"
35
36 /* Plugin callbacks */
37 static int Open (vlc_object_t *);
38 static void Close (vlc_object_t *);
39
40 #define GL_TEXT N_("OpenGL extension")
41 #define GLES2_TEXT N_("OpenGL ES 2 extension")
42 #define GLES_TEXT N_("OpenGL ES extension")
43 #define PROVIDER_LONGTEXT N_( \
44     "Extension through which to use the Open Graphics Library (OpenGL).")
45
46 vlc_module_begin ()
47 #if USE_OPENGL_ES == 2
48 # error The OpenGL ES2 plugin is incomplete and not functional. FIXME.
49 # define API VLC_OPENGL_ES2
50 # define MODULE_VARNAME "gles2"
51     set_shortname (N_("OpenGL ES2"))
52     set_description (N_("OpenGL for Embedded Systems 2 video output"))
53     set_capability ("vout display", /*165*/0)
54     set_callbacks (Open, Close)
55     add_shortcut ("opengles2", "gles2")
56     add_module ("gles2", "opengl es2", NULL,
57                 GLES2_TEXT, PROVIDER_LONGTEXT, true)
58
59 #elif USE_OPENGL_ES == 1
60 # define API VLC_OPENGL_ES
61 # define MODULE_VARNAME "gles"
62     set_shortname (N_("OpenGL ES"))
63     set_description (N_("OpenGL for Embedded Systems video output"))
64     set_capability ("vout display", /*160*/0)
65     set_callbacks (Open, Close)
66     add_shortcut ("opengles", "gles")
67     add_module ("gles", "opengl es", NULL,
68                 GLES_TEXT, PROVIDER_LONGTEXT, true)
69 #else
70 # define API VLC_OPENGL
71 # define MODULE_VARNAME "gl"
72     set_shortname (N_("OpenGL"))
73     set_description (N_("OpenGL video output"))
74     set_category (CAT_VIDEO)
75     set_subcategory (SUBCAT_VIDEO_VOUT)
76     set_capability ("vout display", /*170*/0)
77     set_callbacks (Open, Close)
78     add_shortcut ("opengl", "gl")
79     add_module ("gl", "opengl", NULL,
80                 GL_TEXT, PROVIDER_LONGTEXT, true)
81 #endif
82 vlc_module_end ()
83
84 struct vout_display_sys_t
85 {
86     vout_display_opengl_t *vgl;
87
88     vout_window_t *window;
89     vlc_gl_t *gl;
90     picture_pool_t *pool;
91 };
92
93 /* Display callbacks */
94 static picture_pool_t *Pool (vout_display_t *, unsigned);
95 static void PictureRender (vout_display_t *, picture_t *, subpicture_t *);
96 static void PictureDisplay (vout_display_t *, picture_t *, subpicture_t *);
97 static int Control (vout_display_t *, int, va_list);
98
99 static vout_window_t *MakeWindow (vout_display_t *vd)
100 {
101     vout_window_cfg_t wnd_cfg;
102
103     memset (&wnd_cfg, 0, sizeof (wnd_cfg));
104     wnd_cfg.type = VOUT_WINDOW_TYPE_NATIVE;
105     wnd_cfg.x = var_InheritInteger (vd, "video-x");
106     wnd_cfg.y = var_InheritInteger (vd, "video-y");
107     wnd_cfg.width  = vd->cfg->display.width;
108     wnd_cfg.height = vd->cfg->display.height;
109
110     vout_window_t *wnd = vout_display_NewWindow (vd, &wnd_cfg);
111     if (wnd == NULL)
112         msg_Err (vd, "parent window not available");
113     return wnd;
114 }
115
116 /**
117  * Allocates a surface and an OpenGL context for video output.
118  */
119 static int Open (vlc_object_t *obj)
120 {
121     vout_display_t *vd = (vout_display_t *)obj;
122     vout_display_sys_t *sys = malloc (sizeof (*sys));
123     if (unlikely(sys == NULL))
124         return VLC_ENOMEM;
125
126     sys->gl = NULL;
127     sys->pool = NULL;
128
129     sys->window = MakeWindow (vd);
130     if (sys->window == NULL)
131         goto error;
132
133     sys->gl = vlc_gl_Create (sys->window, API, "$" MODULE_VARNAME);
134     if (sys->gl == NULL)
135         goto error;
136
137     if (vlc_gl_MakeCurrent (sys->gl))
138         goto error;
139
140     /* Initialize video display */
141     sys->vgl = vout_display_opengl_New (&vd->fmt, NULL, sys->gl);
142     if (!sys->vgl)
143         goto error;
144
145     vd->sys = sys;
146     vd->info.has_pictures_invalid = false;
147     vd->info.has_event_thread = false;
148     vd->pool = Pool;
149     vd->prepare = PictureRender;
150     vd->display = PictureDisplay;
151     vd->control = Control;
152     vd->manage = NULL;
153     return VLC_SUCCESS;
154
155 error:
156     if (sys->gl != NULL)
157         vlc_gl_Destroy (sys->gl);
158     if (sys->window != NULL)
159         vout_display_DeleteWindow (vd, sys->window);
160     free (sys);
161     return VLC_EGENERIC;
162 }
163
164 /**
165  * Destroys the OpenGL context.
166  */
167 static void Close (vlc_object_t *obj)
168 {
169     vout_display_t *vd = (vout_display_t *)obj;
170     vout_display_sys_t *sys = vd->sys;
171
172     vout_display_opengl_Delete (sys->vgl);
173     vlc_gl_Destroy (sys->gl);
174     vout_display_DeleteWindow (vd, sys->window);
175     free (sys);
176 }
177
178 /**
179  * Returns picture buffers
180  */
181 static picture_pool_t *Pool (vout_display_t *vd, unsigned count)
182 {
183     vout_display_sys_t *sys = vd->sys;
184
185     if (!sys->pool)
186         sys->pool = vout_display_opengl_GetPool (sys->vgl, count);
187     return sys->pool;
188 }
189
190 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
191 {
192     vout_display_sys_t *sys = vd->sys;
193
194     vout_display_opengl_Prepare (sys->vgl, pic, subpicture);
195 }
196
197 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
198 {
199     vout_display_sys_t *sys = vd->sys;
200
201     vout_display_opengl_Display (sys->vgl, &vd->source);
202     picture_Release (pic);
203     (void)subpicture;
204 }
205
206 static int Control (vout_display_t *vd, int query, va_list ap)
207 {
208     vout_display_sys_t *sys = vd->sys;
209
210     switch (query)
211     {
212       case VOUT_DISPLAY_HIDE_MOUSE: /* FIXME TODO */
213         break;
214 #ifndef NDEBUG
215       case VOUT_DISPLAY_RESET_PICTURES: // not needed
216         assert(0);
217 #endif
218       case VOUT_DISPLAY_CHANGE_FULLSCREEN:
219       {
220         const vout_display_cfg_t *cfg =
221             va_arg (ap, const vout_display_cfg_t *);
222
223         return vout_window_SetFullScreen (sys->window, cfg->is_fullscreen);
224       }
225
226       case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
227       {
228         unsigned state = va_arg (ap, unsigned);
229
230         return vout_window_SetState (sys->window, state);
231       }
232
233       case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
234       case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
235       case VOUT_DISPLAY_CHANGE_ZOOM:
236       {
237         const vout_display_cfg_t *cfg = va_arg (ap, const vout_display_cfg_t *);
238         const video_format_t *src = &vd->source;
239
240         if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
241         {
242             bool force = false;
243
244             force = va_arg (ap, int);
245             if (force
246              && (cfg->display.width  != vd->cfg->display.width
247               || cfg->display.height != vd->cfg->display.height)
248              && vout_window_SetSize (sys->window,
249                                      cfg->display.width, cfg->display.height))
250                 return VLC_EGENERIC;
251         }
252
253         vout_display_place_t place;
254
255         vout_display_PlacePicture (&place, src, cfg, false);
256         glViewport (0, 0, place.width, place.height);
257         return VLC_SUCCESS;
258       }
259
260       case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
261       case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
262       {
263         const vout_display_cfg_t *cfg = vd->cfg;
264         const video_format_t *src = va_arg (ap, const video_format_t *);
265         vout_display_place_t place;
266
267         vout_display_PlacePicture (&place, src, cfg, false);
268         glViewport (0, 0, place.width, place.height);
269         return VLC_SUCCESS;
270       }
271
272       case VOUT_DISPLAY_GET_OPENGL:
273       {
274         vlc_gl_t **pgl = va_arg (ap, vlc_gl_t **);
275
276         *pgl = sys->gl;
277         return VLC_SUCCESS;
278       }
279
280       default:
281         msg_Err (vd, "Unknown request %d", query);
282     }
283     return VLC_EGENERIC;
284 }