]> git.sesse.net Git - vlc/blob - modules/video_output/gl.c
Win32: common code is not DirectX specific
[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 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.
12  *
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.
17  *
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  *****************************************************************************/
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 # define API VLC_OPENGL_ES2
49 # define MODULE_VARNAME "gles2"
50     set_shortname (N_("OpenGL ES2"))
51     set_description (N_("OpenGL for Embedded Systems 2 video output"))
52     set_capability ("vout display", /*165*/0)
53     set_callbacks (Open, Close)
54     add_shortcut ("opengles2", "gles2")
55     add_module ("gles2", "opengl es2", NULL,
56                 GLES2_TEXT, PROVIDER_LONGTEXT, true)
57
58 #elif USE_OPENGL_ES == 1
59 # define API VLC_OPENGL_ES
60 # define MODULE_VARNAME "gles"
61     set_shortname (N_("OpenGL ES"))
62     set_description (N_("OpenGL for Embedded Systems video output"))
63     set_capability ("vout display", /*160*/0)
64     set_callbacks (Open, Close)
65     add_shortcut ("opengles", "gles")
66     add_module ("gles", "opengl es", NULL,
67                 GLES_TEXT, PROVIDER_LONGTEXT, true)
68 #else
69 # define API VLC_OPENGL
70 # define MODULE_VARNAME "gl"
71     set_shortname (N_("OpenGL"))
72     set_description (N_("OpenGL video output (experimental)"))
73     set_category (CAT_VIDEO)
74     set_subcategory (SUBCAT_VIDEO_VOUT)
75     set_capability ("vout display", /*170*/0)
76     set_callbacks (Open, Close)
77     add_shortcut ("opengl", "gl")
78     add_module ("gl", "opengl", NULL,
79                 GL_TEXT, PROVIDER_LONGTEXT, true)
80 #endif
81 vlc_module_end ()
82
83 struct vout_display_sys_t
84 {
85     vout_display_opengl_t *vgl;
86
87     vout_window_t *window;
88     vlc_gl_t *gl;
89     picture_pool_t *pool;
90 };
91
92 /* Display callbacks */
93 static picture_pool_t *Pool (vout_display_t *, unsigned);
94 static void PictureRender (vout_display_t *, picture_t *, subpicture_t *);
95 static void PictureDisplay (vout_display_t *, picture_t *, subpicture_t *);
96 static int Control (vout_display_t *, int, va_list);
97
98 static vout_window_t *MakeWindow (vout_display_t *vd)
99 {
100     vout_window_cfg_t wnd_cfg;
101
102     memset (&wnd_cfg, 0, sizeof (wnd_cfg));
103
104     /* Please keep this in sync with egl.c */
105     /* <EGL/eglplatform.h> defines the list and order of platforms */
106 #if defined(_WIN32) || defined(__VC32__) \
107  && !defined(__CYGWIN__) && !defined(__SCITECH_SNAP__)
108     wnd_cfg.type = VOUT_WINDOW_TYPE_HWND;
109 #elif defined(__WINSCW__) || defined(__SYMBIAN32__)  /* Symbian */
110 # warning Symbian not supported.
111 #elif defined(WL_EGL_PLATFORM)
112 # error Wayland not supported.
113 #elif defined(__GBM__)
114 # error Glamor not supported.
115 #elif defined(ANDROID)
116 # error Android not supported.
117 #elif defined(__unix__) /* X11 */
118     wnd_cfg.type = VOUT_WINDOW_TYPE_XID;
119 #else
120 # error Platform not recognized.
121 #endif
122     wnd_cfg.x = var_InheritInteger (vd, "video-x");
123     wnd_cfg.y = var_InheritInteger (vd, "video-y");
124     wnd_cfg.width  = vd->cfg->display.width;
125     wnd_cfg.height = vd->cfg->display.height;
126
127     vout_window_t *wnd = vout_display_NewWindow (vd, &wnd_cfg);
128     if (wnd == NULL)
129         msg_Err (vd, "parent window not available");
130     return wnd;
131 }
132
133 /**
134  * Allocates a surface and an OpenGL context for video output.
135  */
136 static int Open (vlc_object_t *obj)
137 {
138     vout_display_t *vd = (vout_display_t *)obj;
139     vout_display_sys_t *sys = malloc (sizeof (*sys));
140     if (unlikely(sys == NULL))
141         return VLC_ENOMEM;
142
143     sys->gl = NULL;
144     sys->pool = NULL;
145
146     sys->window = MakeWindow (vd);
147     if (sys->window == NULL)
148         goto error;
149
150     sys->gl = vlc_gl_Create (sys->window, API, "$" MODULE_VARNAME);
151     if (sys->gl == NULL)
152         goto error;
153
154     if (vlc_gl_MakeCurrent (sys->gl))
155         goto error;
156
157     /* Initialize video display */
158     const vlc_fourcc_t *spu_chromas;
159     sys->vgl = vout_display_opengl_New (&vd->fmt, &spu_chromas, sys->gl);
160     if (!sys->vgl)
161     {
162         vlc_gl_ReleaseCurrent (sys->gl);
163         goto error;
164     }
165
166     vd->sys = sys;
167     vd->info.has_pictures_invalid = false;
168     vd->info.has_event_thread = false;
169     vd->info.subpicture_chromas = spu_chromas;
170     vd->pool = Pool;
171     vd->prepare = PictureRender;
172     vd->display = PictureDisplay;
173     vd->control = Control;
174     vd->manage = NULL;
175     return VLC_SUCCESS;
176
177 error:
178     if (sys->gl != NULL)
179         vlc_gl_Destroy (sys->gl);
180     if (sys->window != NULL)
181         vout_display_DeleteWindow (vd, sys->window);
182     free (sys);
183     return VLC_EGENERIC;
184 }
185
186 /**
187  * Destroys the OpenGL context.
188  */
189 static void Close (vlc_object_t *obj)
190 {
191     vout_display_t *vd = (vout_display_t *)obj;
192     vout_display_sys_t *sys = vd->sys;
193
194     vout_display_opengl_Delete (sys->vgl);
195     vlc_gl_ReleaseCurrent (sys->gl);
196
197     vlc_gl_Destroy (sys->gl);
198     vout_display_DeleteWindow (vd, sys->window);
199     free (sys);
200 }
201
202 /**
203  * Returns picture buffers
204  */
205 static picture_pool_t *Pool (vout_display_t *vd, unsigned count)
206 {
207     vout_display_sys_t *sys = vd->sys;
208
209     if (!sys->pool)
210         sys->pool = vout_display_opengl_GetPool (sys->vgl, count);
211     return sys->pool;
212 }
213
214 static void PictureRender (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
215 {
216     vout_display_sys_t *sys = vd->sys;
217
218     vout_display_opengl_Prepare (sys->vgl, pic, subpicture);
219 }
220
221 static void PictureDisplay (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
222 {
223     vout_display_sys_t *sys = vd->sys;
224
225     vout_display_opengl_Display (sys->vgl, &vd->source);
226     picture_Release (pic);
227     (void)subpicture;
228 }
229
230 static int Control (vout_display_t *vd, int query, va_list ap)
231 {
232     vout_display_sys_t *sys = vd->sys;
233
234     switch (query)
235     {
236       case VOUT_DISPLAY_HIDE_MOUSE: /* FIXME TODO */
237         break;
238 #ifndef NDEBUG
239       case VOUT_DISPLAY_RESET_PICTURES: // not needed
240         assert(0);
241 #endif
242       case VOUT_DISPLAY_CHANGE_FULLSCREEN:
243       {
244         const vout_display_cfg_t *cfg =
245             va_arg (ap, const vout_display_cfg_t *);
246
247         return vout_window_SetFullScreen (sys->window, cfg->is_fullscreen);
248       }
249
250       case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
251       {
252         unsigned state = va_arg (ap, unsigned);
253
254         return vout_window_SetState (sys->window, state);
255       }
256
257       case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
258       case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
259       case VOUT_DISPLAY_CHANGE_ZOOM:
260       {
261         const vout_display_cfg_t *cfg = va_arg (ap, const vout_display_cfg_t *);
262         const video_format_t *src = &vd->source;
263
264         if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
265         {
266             bool force = false;
267
268             force = va_arg (ap, int);
269             if (force
270              && (cfg->display.width  != vd->cfg->display.width
271               || cfg->display.height != vd->cfg->display.height)
272              && vout_window_SetSize (sys->window,
273                                      cfg->display.width, cfg->display.height))
274                 return VLC_EGENERIC;
275         }
276
277         vout_display_place_t place;
278
279         vout_display_PlacePicture (&place, src, cfg, false);
280         glViewport (0, 0, place.width, place.height);
281         return VLC_SUCCESS;
282       }
283
284       case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
285       case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
286       {
287         const vout_display_cfg_t *cfg = vd->cfg;
288         const video_format_t *src = va_arg (ap, const video_format_t *);
289         vout_display_place_t place;
290
291         vout_display_PlacePicture (&place, src, cfg, false);
292         glViewport (0, 0, place.width, place.height);
293         return VLC_SUCCESS;
294       }
295
296       case VOUT_DISPLAY_GET_OPENGL:
297       {
298         vlc_gl_t **pgl = va_arg (ap, vlc_gl_t **);
299
300         *pgl = sys->gl;
301         return VLC_SUCCESS;
302       }
303
304       default:
305         msg_Err (vd, "Unknown request %d", query);
306     }
307     return VLC_EGENERIC;
308 }